Home  >  Article  >  Backend Development  >  php中单双引号问题

php中单双引号问题

WBOY
WBOYOriginal
2016-06-06 20:34:441065browse

代码1:

<code>PHP</code><code>$str = "我是一个php程序员";
$length = strlen(preg_replace("/[\x00-\x7F]/", '', $str));
echo $length;
</code>

输出0并提示Warning: preg_replace(): Null byte in regex in

代码2:

<code>PHP</code><code>$str = "我是一个php程序员";
$length = strlen(preg_replace('/[\x00-\x7F]/', '', $str));
echo $length;
</code>

输出21

两处代码唯一的不一样就是preg_replace的第一个参数是单引号还是双引号。求解释!

回复内容:

代码1:

<code>PHP</code><code>$str = "我是一个php程序员";
$length = strlen(preg_replace("/[\x00-\x7F]/", '', $str));
echo $length;
</code>

输出0并提示Warning: preg_replace(): Null byte in regex in

代码2:

<code>PHP</code><code>$str = "我是一个php程序员";
$length = strlen(preg_replace('/[\x00-\x7F]/', '', $str));
echo $length;
</code>

输出21

两处代码唯一的不一样就是preg_replace的第一个参数是单引号还是双引号。求解释!

单引号不会对反斜杠\转义, 双引号会对反斜杠\转义.

第一部分写成:
$str = "我是一个php程序员";
$length = strlen(preg_replace("/[\\x00-\\x7F]/", '', $str));
echo $length;

就不会出问题了

和单双引号没有关系,是你那个ascii的\没有转义。。。\\就可以了

Statement:
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