1

I try get this from css files:

url(/myServer/img/mosaico.png)

and I try with this:

preg_match_all('/url\((["\']?)((?!["\']?[data:]?).*?\.(gif|png|jpg|jpeg|svg|woff|eot|ttf))\\1\)?/i', $content, $matches, PREG_SET_ORDER))

But it not works property,the expression only captures some occurrences. What is the problem?

I also need to get the parts in brackets in regular expresion, like this:

array(
    (int) 0 => 'url(/img/arrow-top-white.png)',
    (int) 1 => '',
    (int) 2 => '/img/arrow-top-white.png',
    (int) 3 => 'png'
)
Martin
  • 1,282
  • 1
  • 15
  • 43

1 Answers1

2

You could use the below regex to match all the strings which are mentioned above.

url\(((?:\/[^\/()]+?)+?\.(gif|png|jpg|jpeg|svg|woff|eot|ttf))[^)]*\)

DEMO

<?php
$content = "url(/myServer/img/mosaico.png)";
preg_match_all('~url\(((?:\/[^\/]+)+\.(gif|png|jpg|jpeg|svg|woff|eot|ttf))\)~', $content, $matches, PREG_SET_ORDER);
print_r($matches);
?>

Output:

Array
(
    [0] => Array
        (
            [0] => url(/myServer/img/mosaico.png)
            [1] => /myServer/img/mosaico.png
            [2] => png
        )

)
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • yes it works, but I need to try to get the parts in the parentheses – Martin Sep 29 '14 at 16:52
  • That does not work properly, do not get the extension of the file(not very important) but only finds two occurrences – Martin Sep 29 '14 at 17:02
  • It return this: (int) 0 => array( (int) 0 => 'url(/img/bodyTexture.png)', (int) 1 => /img/bodyTexture.png' ), (int) 1 => array( (int) 0 => 'url(/img/mosaico.png) no-repeat center center fixed; color:#fff; background-size:cover; – Martin Sep 29 '14 at 17:04
  • 1
    you mean this http://regex101.com/r/nA6hN9/22 . If you have further queries then edit your question and add the expected output. – Avinash Raj Sep 29 '14 at 17:04
  • thanks for your time, i added the output that i expect in question. I therefore positively rated yourresponse, but is not the solution that i need :) – Martin Sep 29 '14 at 17:08
  • why you want the second one as empty? – Avinash Raj Sep 29 '14 at 17:09
  • The last regex that you put get all parts that i need, but it not work correctly. Test your expresion with this text : url(/var/www/synergicNetwork/app/webroot/img/mosaico.png) no-repeat center center fixed; color:#fff; background-size:cover; url(/var/www/synergicNetwork/app/webroot/img/mosaico.png) no-repeat center center fixed; color:#fff; background-size:cover; – Martin Sep 29 '14 at 17:12
  • It works, only fails with this string url(/ghfdgh/glyphicons-halflings-regular.eot?#iefix) format('embedded-opentype'),url(/fonts/glyphicons-halflings-regular.woff) – Martin Sep 29 '14 at 17:20
  • 1
    here http://regex101.com/r/nA6hN9/25 . I think you don't want to match the url string which has no extensions. – Avinash Raj Sep 29 '14 at 17:22
  • it is that I needed '/url\(((?:\/[^\/()]+?)+?\.(gif|png|jpg|jpeg|svg|woff|eot|ttf))[^)]*\)/im'it similar yours but not the same, edit your answer and I accept a close this :-). And really thanks – Martin Sep 29 '14 at 21:39
  • 1
    updated... I thought that you don't want to match the url if it's don't have the exact extension. – Avinash Raj Sep 30 '14 at 03:47