views:

56

answers:

4

When i wrote the following, i get such warning, and it doesn't match anything, but i'm sure, that $row_search['content'] contains $value...

preg_match_all("/[^\s]*".preg_quote($value)."[^\s]*/iu", $row_search['content'], $final_matched);

WARNING:preg_match_all() [function.preg-match-all]: Unknown modifier '/' in C:\xampp\htdocs\finance\search\search.php on line 82

why i get such warning message?

Thanks

A: 

Specify / as delimiter when using preg_quote:

preg_match_all("~/[^\s]*~".preg_quote($value, '/')."[^\s]*/iu", $row_search['content'], $final_matched);

Otherwise a / in the value of $value will not get escaped.

Gumbo
+4  A: 

You should change your preg_quote($value) into preg_quote($value, "/") to escape the delimiter too.

In your example if $value contains "(content/other)" it will be escaped as "(content/other)" and this in your regex will be /[^\s]*\(content/other\)[^\s]*/iu as you can see there is a / which will case your regex to crash. If you explicitly say that "/" is the delimiter then the regex will be /[^\s]*\(content\/other\)[^\s]*/iu

Colin Hebert
+4  A: 

I assume $value contains a slash /, which is not escaped by preg_quote:

The special regular expression characters are: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -

Pass the delimiter you use to the function:

preg_match_all("/[^\s]*".preg_quote($value, '/')."[^\s]*/iu", $row_search['content'], $final_matched);
//                                        ---^

or use another delimiter.

Felix Kling
+2  A: 

/ is not a PCRE metacharacter, so although you're using it as the delimiter in your regular expression, preg_quote() won't escape it because it doesn't know you're using it as the delimiter. You'll have to pass it in as the second parameter, as the rest have said, in order for it to be escaped too:

preg_match_all("/[^\s]*".preg_quote($value, '/')."[^\s]*/iu", $row_search['content'], $final_matched);
BoltClock