Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Preg match - get wrapped value

I d like to extract all wrapped text value using preg match

So

background: url("images/gone.png");
color: #333;
...

background: url("images/good.png");
font-weight: bold;

From above string, I want to grab

images/gone.png
images/good.png

What will be the right command line for this?

like image 977
Codemole Avatar asked May 21 '26 23:05

Codemole


2 Answers

In php, you should this:

$str = <<<CSS
    background: url("images/gone.png");
    color: #333;

    background: url("images/good.png");
    font-weight: bold;
CSS;

preg_match_all('/url\("(.*?)"\)/', $str, $matches);
var_dump($matches);

Then, you will see something like this as an output:

array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(22) "url("images/gone.png")"
    [1]=>
    string(22) "url("images/good.png")"
  }
  [1]=>
  array(2) {
    [0]=>
    string(15) "images/gone.png"
    [1]=>
    string(15) "images/good.png"
  }
}

Therefore, the list with the urls will be in $matches[1] :)

like image 145
Yuri Teixeira Avatar answered May 23 '26 12:05

Yuri Teixeira


http://www.phpliveregex.com/p/e3u

This regex will do that:

/^background: url\("(.*?)"\);$/

Better learn some about regex, its worth the time: http://regexone.com/

like image 44
user5542121 Avatar answered May 23 '26 14:05

user5542121



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!