2

I'm trying to get CSS background-image URL from HTML Attribute using Simple HTML DOM . This the codes

$String=' <div class="Wrapper"><i style="background-image: url(https://example.com/backgroud-1035.jpg);" class="uiMediaThumbImg"></i></div>';
$html = new simple_html_dom();
$html->load($String); 
foreach($html->find('i') as $a0)
$src[$i++]=$a0->style;
foreach( $src as $css  ) 
print($css);

The output is Like this :-

background-image: url(https://example.com/backgroud-1035.jpg); 

All I want is strip background Url from the rest of CSS tags . Like this https://example.com/backgroud-1035.jpg

2
  • This isn't doable with php dom objects alone. Style is the attribute, and any styles in that attribute are simply the value of that attribute. You will need to figure another way of parsing this value out of the output you currently have. Perhaps a regex statement, as ugly as that might be. Even then you'll encounter a variety of ways that someone can set a background image in css , sometimes quotations (and either ' or "), this will only work in your one scenario. Commented Jun 25, 2015 at 0:07
  • Yest it need preg_match_all or preg_match I'm not that expert with it always confuse me . Commented Jun 25, 2015 at 0:25

2 Answers 2

8

You can use regex to strip out the text between parentheses.

foreach($html->find('i') as $a0){
    $style = $a0->style;
    preg_match('/\(([^)]+)\)/', $style, $match);
    $src[$i++] = $match[1];
    //echo $match[1];
}
Sign up to request clarification or add additional context in comments.

4 Comments

I just found the solution Via preg_match_all . I'll try your solution thanks
preg_match_all tries to match all possible matches within an entire string, maybe more than once match. preg_match on the other hand, doesn't check for any possible match after it finds a match. so as your strings that you are checking will always contain one possible match, then it won't matter whether you use preg_match or preg_match_all, what matters more is what regex you use in this case.
yes your solution works perfect this is my code preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $css, $match); and yest I need one returning value , preg_match do it .
your code looks for a valid link within a string while my answer looks for the string within parentheses, both will work.
1

Maybe you found the answer, but for who did not I will use the explode() function so I can break the string into an array, see more about explode() function here. First I splited the $css variable into array. the array be like this:

background-image: url(https://example.com/backgroud-1035.jpg);" 
Array ( [0] => background-image: [1] => https://example.com/backgroud-1035.jpg);

And then break the ['1'] into array, then it looks like this

Array ( [0] => https://example.com/backgroud-1035.jpg [1] => ; )

And then print the ['0'] .

// it will output https://example.com/backgroud-1035.jpg 

full code:

    $String=' <div class="Wrapper"><i style="background-image: 
    url(https://example.com/backgroud-1035.jpg);" class="uiMediaThumbImg"></i></div>';
    $html = new simple_html_dom();
    $html->load($String); 
    foreach($html->find('i') as $a0)
    $src[$i++]=$a0->style;
    foreach( $src as $css  ) 
    
    $explode1 = explode("url(",$css);
    $explode2 = explode(")",$explode1['1']);
    print_r ($explode2['0']);

2 Comments

Could you please provide an explanation for your answer?
Welcome to Stack Overflow. Code-only answers are discouraged on Stack Overflow because they don't explain how it solves the problem. Please edit your answer to explain what this code does and how it answers the question, so that it is useful to the OP as well as other users with similar issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.