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

php中单双引号问题

WBOY
Release: 2016-06-06 20:34:44
Original
1103 people have browsed it

代码1:

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

输出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>
Copy after login
Copy after login

输出21

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

回复内容:

代码1:

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

输出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>
Copy after login
Copy after login

输出21

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

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

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

就不会出问题了

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

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