PHP: string to regex

sultan picture sultan · Mar 26, 2012 · Viewed 9.7k times · Source

I try use string as a regular expression pattern but I've following errors

PHP Warning:  preg_match(): Unknown modifier '>' in /Applications/MAMP/htdocs/cruncher/Plugins/wordpress/WPDetect.php on line 22
PHP Warning:  preg_match(): Unknown modifier '/' in /Applications/MAMP/htdocs/cruncher/Plugins/wordpress/WPDetect.php on line 22

The code

$str = "<meta name=\"generator\" content=\"WordPress.com\" />"
preg_match("/".$str."/", $content->content)

I also tried to use preg_quote function but I've similar problems.

What is the correct way to make it work?

Sultan

Answer

piotrekkr picture piotrekkr · Mar 26, 2012

Use preg_quote function and pattern enclosed with |...|

preg_match("|" . preg_quote($str, "|") . "|", $content->content)