为什么php中的*会匹配到null,是php的bug吗,还是其它语言也会这样?

WBOY
Release: 2016-06-06 20:24:52
Original
1164 people have browsed it

<code>php > preg_match_all('/.*/',null,$arr);
php > print_r($arr);
Array
(
    [0] => Array
        (
            [0] => 
        )

)
php > var_dump($arr);
array(1) {
  [0]=>
  array(1) {
    [0]=>
    string(0) ""
  }
}
php > preg_match_all('/.+/',null,$arr);
php > var_dump($arr);
array(1) {
  [0]=>
  array(0) {
  }
}
php > preg_match_all('/.*/U','Love',$arr);
php > var_dump($arr);
array(1) {
  [0]=>
  array(9) {
    [0]=>
    string(0) ""
    [1]=>
    string(1) "L"
    [2]=>
    string(0) ""
    [3]=>
    string(1) "o"
    [4]=>
    string(0) ""
    [5]=>
    string(1) "v"
    [6]=>
    string(0) ""
    [7]=>
    string(1) "e"
    [8]=>
    string(0) ""
  }
}

</code>
Copy after login
Copy after login

回复内容:

<code>php > preg_match_all('/.*/',null,$arr);
php > print_r($arr);
Array
(
    [0] => Array
        (
            [0] => 
        )

)
php > var_dump($arr);
array(1) {
  [0]=>
  array(1) {
    [0]=>
    string(0) ""
  }
}
php > preg_match_all('/.+/',null,$arr);
php > var_dump($arr);
array(1) {
  [0]=>
  array(0) {
  }
}
php > preg_match_all('/.*/U','Love',$arr);
php > var_dump($arr);
array(1) {
  [0]=>
  array(9) {
    [0]=>
    string(0) ""
    [1]=>
    string(1) "L"
    [2]=>
    string(0) ""
    [3]=>
    string(1) "o"
    [4]=>
    string(0) ""
    [5]=>
    string(1) "v"
    [6]=>
    string(0) ""
    [7]=>
    string(1) "e"
    [8]=>
    string(0) ""
  }
}

</code>
Copy after login
Copy after login

在PHP里面 . .表示任意字符 表示任意位 ,既然是任意位 0位也可以,null,不匹配任意字符,那正好是0位,10位也是任意位,所以是你没有正则理解正则

php是门弱类型语言, preg_match的第二个参数是字符串, 所以null被直接解析成了空字符串。

https://github.com/php/php-src/blob/PHP-5.6.16/ext/pcre/php_pcre.c#L569

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template