0
Warning: preg_match_all() [function.preg-match-all]: Unknown modifier 'l' in /var/www/test.php on line 9

It's saying my regex has an unknown modifier, but I'm not sure what exactly is happening to trigger the error

preg_match_all("/\<select id\=\'subscription_division_id\'(.+?)</select>\/is", $html, $matches);
RichieHindle
  • 272,464
  • 47
  • 358
  • 399
Ben
  • 60,438
  • 111
  • 314
  • 488

2 Answers2

5

You are escaping wrong. For the regex parser, the following is your regex:

\<select id\=\'subscription_division_id\'(.+?)<

whereas select>\/is is supposed to be the regexp modifiers (the regex string is enclosed in /). Given that the l in there is the first invalid modifier, you receive that error. So to fix that, you need to escape the slash in the closing tag. And btw. you are escaping quite a lot unnecessary things, this is enough:

preg_match_all("/<select id='subscription_division_id'(.+?)<\/select>/is", $html, $matches);
poke
  • 369,085
  • 72
  • 557
  • 602
  • 3
    +1 Using a different delimiter (typically `~`) would be even better, so no escaping is required at all. – NikiC Jan 27 '11 at 19:23
3

PHP’s PCRE functions require the pattern to be delimited by delimiters that separate the pattern from optional modifiers. But these delimiters need to be escaped if they occur inside the pattern. So you need to escape the delimiters / inside your pattern:

"/\<select id\=\'subscription_division_id\'(.+?)<\/select>/is"
                                                  ^

Otherwise the pattern is ended prematurely and the rest is interpreted as modifiers. Like in your case where the rest (i.e. select>/is) is interpreted as such. s and e are valid modifiers but l isn’t. That’s the reason for your error message.

Gumbo
  • 643,351
  • 109
  • 780
  • 844