0

I'm trying to grab all url's in a big css file.

I tried using this regex:

preg_match_all("/url\('(\\'|[^'])*'\)/m", $css, $matches);

$css is a 10kB variable, read from a css file using file_get_contents.
$matches is an empty array.

However, this results in a (net::ERR_EMPTY_RESPONSE) error in chrome: the server doesn't send any data. If i return the function just before this line, everything works fine. If i return the function just below this line, I get an empty response.

It doesn't even send the data I outputted before that line, and it responds (with an empty response) within a few seconds - nowhere near the 30s timeout.

It worked just fine when I first used this regex, and passed the output to the above code. preg_match_all("/@font-face{[^}]+}/m", $css, $matches);
That row was before I realized I needed the other URL's - not only the fonts.

Does anyone have any idea what went wrong?

UPDATE: Could it be this bug?
https://bugs.php.net/bug.php?id=62049
links to:
http://codepad.org/FmlJi8N9
and a quick google search later:
Warning: preg_match(): Internal pcre_fullinfo()

How can I fix this?

Community
  • 1
  • 1
Filip Haglund
  • 13,919
  • 13
  • 64
  • 113

1 Answers1

1

If I understand correctly, you want to do this:

preg_match_all("/url\([^)]*\)/m", $css, $matches);
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • That worked! Do you see any reason to why my crappy regex failed? – Filip Haglund Jul 20 '13 at 21:26
  • Well, mine is a simple greedy expression, will read just as match as needed, so there's no chance for a timeout. Not quite sure about yours... what was your intention with `\\'`? – Karoly Horvath Jul 20 '13 at 21:32
  • To allow escaped quotes in the path. I noticed that we're using a combination of ', " and no quotes, so I modified the code to match that as well. Your regex fits nicely into the new code. – Filip Haglund Jul 20 '13 at 21:38