12

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

4 Answers 4

16

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

preg_match("|" . preg_quote($str, "|") . "|", $content->content)
Sign up to request clarification or add additional context in comments.

2 Comments

Ok sorry did'n tknow there was a time limit :)
This will fail as soon as $str contains a |. My advice is to always supply the second argument. In this case: '|'.preg_quote($str, '|').'|'
6

This worked for me

$pattern = "/" . preg_quote($source, "/") . "/";

Comments

0

You must escape your limiter

$str = "<meta name=\"generator\" content=\"WordPress.com\" \/>"

Comments

0

Regular expression contain a set of special char like \ - * . ? $ ^ + () []and more, you have to escape them from your string before using it (you esacpe by adding a \ before the char)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.