1

I'm having the following reg-ex which is working fine for the normal string match against an array,

preg_grep( "/^". $name . "$/i", $values);

However its not working for the string which has special characters like "Entertainment (General)".

Find a related thread however its for java script and also it didn't help.

1 Answer 1

5

Use the preg_quote function to escape any special characters that might be in the string:

preg_grep( "/^". preg_quote($name, '/') . "$/i", $values);

From the documentation:

preg_quote() puts a backslash in front of every character that is part of the regular expression syntax. This is useful if you have a run-time string that you need to match in some text and the string may contain special regex characters.

1
  • Ah. how I missed that functio? :(. Thanks for the very quick response :) Dec 15, 2012 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.