user6670476
user6670476

Reputation: 153

Differenc between escaping slash with one/two backslash in preg_match() in PHP?

In regular expression sometimes I encountered the forward slash, I have tried to escape it using one backslash and two backslashes and realize the return is the same. So I would like to understand it more clearly.

Example code:

var_dump(preg_match("/^\\/$/D", "/"));

var_dump(preg_match("/^\/$/D", "/"));

They both give the same output

int(1)

In my understanding escaping it with two backslashes seem correct because when PHP parse the string the first backslash(\) escape the second backslash(\) , and when parsing the regular expression the backslash(\) then escape the forward slash(/).

Is there any difference between them? Thank you.

Upvotes: 0

Views: 475

Answers (1)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89584

That's exactly the same.

In a quoted or an heredoc string you can figure a literal backslash with one or two backslashes. This particularity is due to the fact that the backslash is used as escape character for these kind of strings. You can display the strings to have confirmation of this fact.

In fine, the regex engine receives only one backslash that escapes the delimiter / in the two cases.

Note: to figure a literal backslash for a pattern written in quoted string you need 3 or 4 backslashes (because the backslash is the escape character for the pattern too and must be escaped a second time):

var_dump(preg_match('~^a\\\o$~', 'a\o'));
var_dump(preg_match('~^a\\\\o$~', 'a\o'));
var_dump(preg_match('~^a\\\o$~', 'a\\o'));
var_dump(preg_match('~^a\\\\o$~', 'a\\o'));

As any other character, to figure a backslash in quoted string you only need to write it once, but since this character is used to escape, you need sometime to write two backslashes for disambiguation, in particular when an escape sequence follows. Examples:

echo "a\b"; # a\b
echo 'a\b'; # a\b
echo "a\\b"; # a\b
echo 'a\\b'; # a\b

echo "a\tb";   # a    b
echo "a\\tb";  # a\tb
echo "a\\\tb"; # a\    b

echo 'a\'b';   # a'b
echo 'a\\'b';  # parse error
echo 'a\\\'b'; # a\'b

Note: The only wysiwyg kind of string is the nowdoc string:

echo <<<'EOD'
\\
EOD;

# \\

Upvotes: 2

Related Questions