Two different syntax works the same way...

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Two different syntax works the same way...

Post by kaisellgren »

Hi all,

I can't notice any differences with these codes

Code: Select all

preg_match("/\\[/",$str);
and

Code: Select all

preg_match("/\[/",$str);
They are returning same results. The both match character [ literally. What's the difference?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Technically, the second is incorrect syntax, however they end up being the same thing to PCRE. This is due to PHP parsing backslashes as standard escape characters, then PCRE parsing them again. If you wish to have a backslash in the pattern use four of them.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Post by kaisellgren »

Ah ok thank you. :)
Post Reply