tags:

views:

2475

answers:

2

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);

Thanks

+8  A: 

You are lacking the regexp delimiters. Try:

$pattern = "#<img([^>]*[^/])>#i";
PEZ
That was it, thanks.
Cool. Please take a look at Waquo's answer to where he explains a bit more about it. I choose to use non-slash delimiters since I hate escaping.
PEZ
+6  A: 

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([^>]*[^\\/])>/";
Waquo
The error was caused by the other ]. PHP takes the first character '<' as the opening delimiter, and then the regex 'stops' at the first '>'. So, it's the ']' after that first '>' which is giving that error message
Gareth
Argh. I hate delimiters. They really have no place in a language where the string literal naturally delimits the regex.
Waquo
I beg to differ. Being able to choose the delimiters saves you from backslashtitis.
PEZ
Just "pattern" instead of "/pattern/" oder "#pattern#" should be enough. " is a perfectly fine delimiter. Modifiers could go in a separate parameter.Delimiters make sense in Perl, where regular expressions are part of the language syntax and not in strings.
Waquo