Home » Php » php – preg_match_all() [function.preg-match-all]: Unknown modifier ']'

php – preg_match_all() [function.preg-match-all]: Unknown modifier ']'

Posted by: admin July 12, 2020 Leave a comment


Using a few different patterns but they each come up with this error – so what’s wrong?

My shortest one to diagnose is:

$pattern = "<img([^>]*[^/])>";
preg_match_all($pattern, $subject, $matches);


How to&Answers:

You are lacking the regexp delimiters. Try:

$pattern = "#<img([^>]*[^/])>#i";


A single slash is the default delimiter, which is why the character after it in your original regex was in the error-message.
Using the traditional slashes as delimiters and escaping the slash that is not a delimiter would look like this:

$pattern = "/<img([^>]*[^\/])>/";