7

I'm trying to use preg_match to return all the URL's that are inclosed in " " in a page source code.

The code I am using is

preg_match('"http://(.+?)\"', $code, $matches);

And I am getting the following error:

Warning: preg_match() [function.preg-match]: Unknown modifier '/' in .... on line 13
ron8
  • 243
  • 2
  • 5
  • 13

2 Answers2

19
preg_match('~"http://(.*)"~iU', $code, $matches);

Your issue was you need delimiters (I chose ~) to use with the pattern. See the preg_match() man page for more information.

Jim
  • 18,673
  • 5
  • 49
  • 65
  • 2
    Wait, seriously? You can chose whatever delimiter you want? I've only ever used `/` and there are definitely cases (like this) where using something else would be so much more convenient! +1! – Jeffrey Blake Jul 25 '10 at 03:25
  • Thanks... Worked like a charm! – ron8 Jul 25 '10 at 03:29
  • 2
    @JGB146: Well, *almost* any. You can choose any non-alphanumeric, non-backslash, non-whitespace character as delimiter. – Daniel Egeberg Jul 25 '10 at 10:29
  • ~ is particularly useful if you're matching url-encoded strings as ~ is a character that doesn't need encoding and won't be in the regex, i.e. [^!#$&'()*+,/:;=?@[\\]]+ – Mark Rose Sep 17 '12 at 15:28
-2

Try this:

preg_match('"http:\/\/(.+?)\"', $code, $matches);
Hauleth
  • 22,873
  • 4
  • 61
  • 112