tags:

views:

80

answers:

4

Trying to find "\[" in my content don't get any results. I use this pattern:

preg_replace('/\\\[/', $content, $matches);

What's wrong whith my pattern? Thank you.

Update. My fault, I meant preg_match_all.

+6  A: 

preg_replace is replacing all found matches. Use preg_match for the first match and preg_match_all to get all matches instead:

preg_match_all('/\\\[/', $content, $matches);
Gumbo
@Asaph: There is no need to predefine `$matches` since no read access is happening just a write access.
Gumbo
+1  A: 
preg_replace('/\\\\[/', $content, $matches);

Try 4 (four) backslashes, because the pattern you get with three is "\[" which means just "[" to the regexp engine. :)

bisko
A: 

I've found the right one [\\\\][\[].

Margaret
A: 

Try \\\\\\[, should work.

Because those two characters have special meaning in regexp they have to be masked like this: \\\[. Now when we are putting this regexp into a string for php every backslash has to be masked again because backslash has a special meaning in a string.

serg