Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm messing around with CSS files, and I'm trying to traverse through a CSS file using PHP. What I'm looking for is capturing any image path within a url() selector, using regex (any faster ways you know?).

So far I was able to find this expression: url(([^)]+))
but that one isn't 100% what I'm looking for. I need an expression that finds the url selector and captures anything inside it, meaning if the code includes quotes or single quotes, they will not be captured. e.g: url("images/sunflower.png") the captured string should only be: images/sunflower.png

Appreciate the help.

share|improve this question
    
This is not as simple as it may first appear. The URL may or may not be enclosed within 'single' or "double" quotation marks and the URL itself may or may not contain matching or not-matching parentheses. However, if the CSS file is of your own creation and you know its makeup, then that is a different story. –  ridgerunner Jun 15 '13 at 13:21
add comment

3 Answers

up vote 1 down vote accepted

Try this out for size. It will not work on strings which start with url( but if you are parsing actual CSS then it is invalid to start without a selector or property anyway.

$data =' #foo { background: url("hello.jpg"); } #bar { background: url("flowers/iris.png"); }';
$output = array();
foreach(explode("url(", $data) as $i => $a) { // Split string into array of substrings at boundaries of "url(" and loop through it
    if ($i) {
        $a = explode(")", $a); // Split substring into array at boundaries of ")"
        $url = trim(str_replace(array('"',"'"), "", $a[0])); // Remove " and ' characters
        array_push($output, $url);
    }
} 
print_r($output);

Outputs:

Array ( [0] => hello.jpg [1] => flowers/iris.png )
share|improve this answer
    
That is exactly what I needed. Thank you so much :) –  elad.chen Jun 15 '13 at 13:36
add comment

Please do not reinvent the wheel, where you can avoid...

There are a multitude of CSS parsers that are freely available on the internet. If you are keen to know how it is done, crack open one of the open source ones and see how it is done. Here's an example that took 2 minutes to find:

https://github.com/sabberworm/PHP-CSS-Parser#value

I have pointed you to the part that actually shows how to extract the URL too.

share|improve this answer
    
Thanks. I'm aware of that, Nevertheless, I want to avoid adding to much code for a simple task. All I need is that regex, since it will be used for a very small bit of css file, I consider using a dedicated parser an overkill. –  elad.chen Jun 15 '13 at 12:58
    
Yes, it is overkill for your purposes, but it might be wise to check how that library parses the CSS. this will tell you how to do it yourself. –  bPratik Jun 15 '13 at 13:04
add comment

While I agree with bPratik's answer, perhaps all you need is:

preg_match('/url\([\'"]?([^)]+?)[\'"]?\)/', 'url("images/sunflower.png")', $matches);

/**
url\( matches the url and open bracket
[\'"]+? matches a quote if there is one
([^]+?) matches the contents non-greedy (so the next closing quote wont get stolen)
[\'"]? matches the last quote
) matches the end.
*/

var_dump($matches);
array(2) {
  [0]=>
  string(27) "url("images/sunflower.png")"
  [1]=>
  string(20) "images/sunflower.png"
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.