问题
I have this code
<?php
$a = "\\u0000";
$b = preg_quote($a);
echo "<br />my own: ".$a;
echo "<br />with preg_quote:". $b;
?>
result is here
How is possible that one \
character vanished from my $a
variable?
I think it's very begginer behavior/question but I'm really lost about these escape characters.
Disclaimer: I'm not begginer in PHP
回答1:
because \
escapes the next one, as every \
needs to be escaped.
with a single \ it escapes the next character witch is u, but \u isn't a char code so it is displayed like a simple character
回答2:
because \
does have a meaning in string.
i.e. \n
is the char code for a new line
so \\
is the escaped version of a backslash and evaluates to \
回答3:
$a="\\";
// $a
is \
now, because first \
escape another \
all is OK
回答4:
in $a the double-backslash is evalutated as an escaped backslash, leads to one backslash in output.
in $b preg_quote will escape both backslashes as they are regex-meta-characters. leads to four backslashes in a row, leads to two backslashes in your output.
回答5:
\\
is the escaped version of \
. So, the original string, when printed, only displays a single \
. However, preg_quote
escapes the \
character with another \
and thereby results in \\
.
来源:https://stackoverflow.com/questions/6731660/strange-preg-quote-behavior