Asked  1 Year ago    Answers:  5   Viewed   27 times

how should parse with php (simple html dom/etc..) background and other images of webpage?

case 1: inline css

<div id="id100" style="background:url(/mycar1.jpg)"></div>

case 2: css inside html page

<div id="id100"></div>

<style type="text/css">

case 3: separate css file

<div id="id100" style="background:url(/mycar1.jpg);"></div>



case 4: image inside img tag

solution to case 4 as he appears in php simple html dom parser:

// create dom from url or file
$html = file_get_html('');

// find all images
foreach($html->find('img') as $element)
       echo $element->src . '<br>';

please help me to parse case 1,2,3.

if exist more cases please write them, with soltion if you can please.




for case 1:

// create dom from url or file 
$html = file_get_html('');

// get the style attribute for the item
$style = $html->getelementbyid("id100")->getattribute('style');

// $style = background:url(/mycar1.jpg)
// you would now need to put it into a css parser or do some regular expression magic to get the values you need.

for case 2/3:

// create dom from url or file
$html = file_get_html('');

// get the style element
$style = $html->find('head',0)->find('style');

// $style now contains an array of style elements within the head. you will need to work out using attribute selectors what whether an element has a src attribute, if it does download the external css file and parse (using a css parser), if it doesnt then pass the innertext to the css parser.
Thursday, April 1, 2021

did you try it? try this example (sample: adding data tags).

include 'simple_html_dom.php';

$html_string = '
    <p class="myelems">text inside 1</p>
    <p class="myelems">text inside 2</p>
    <p class="myelems">text inside 3</p>
    <p>simple text 1</p>
    <p>simple text 2</p>

$html = str_get_html($html_string);
foreach($html->find('div p[class="myelems"]') as $key => $p_tags) {
    $p_tags->{'data-index'} = $key;

echo htmlentities($html);


    <p class="myelems" data-index="0">text inside 1</p> 
    <p class="myelems" data-index="1">text inside 2</p> 
    <p class="myelems" data-index="2">text inside 3</p> 
    <p>simple text 1</p> 
    <p>simple text 2</p> 
Friday, May 28, 2021

proceed step by step...

start by getting all the wanted info from one page (the 1st for example)... the idea is to:

  • get all phone blocks: $phones = $html->find('a[data-id]');
  • in a loop, get the wanted info (name, price) from each block
  • insert these info in the db (i cant help with db since i didnt use one for a while, but you can do this on your own it's not that hard)

now that you have the code working for one page, let's try to make it work for all pages knowing that:

  • all pages have the same structure, so we can extract data with the same method/code above
  • the link of the next page to scrape is included in the next button, so we'll stop when this link cannot be found

so here's a code summarizing all what we said above:

$url = "";

// start from the main page
$nextlink = $url;

// loop on each next link as long as it exsists
while ($nextlink) {
    echo "<hr>nextlink: $nextlink<br>";
    //create a dom object
    $html = new simple_html_dom();
    // load html from a url

    /// get phone blocks and extract info (also insert to db) ///
    $phones = $html->find('a[data-id]');

    foreach($phones as $phone) {
        // get the link
        $linkas = $phone->href;

        // get the name
        $pavadinimas = $phone->find('span[class=inner]', 0)->plaintext;

        // get the name price and extract the useful part using regex
        $kaina = $phone->find('span[class=price]', 0)->plaintext;
        // this captures the integer part of decimal numbers: in "123,45" will capture "123"... use @([d,]+),?@ to capture the decimal part too
        preg_match('@(d+),?@', $kaina, $matches);
        $kaina = $matches[1];

        echo $pavadinimas, " #----# ", $kaina, " #----# ", $linkas, "<br>";

        // insert into db here
        // code
        // ...

    // extract the next link, if not found return null
    $nextlink = ( ($temp = $html->find('div.pagination a[class="next"]', 0)) ? "".$temp->href : null );

    // clear dom object


samsung phone i9300 galaxy siii juodas #----# 1099 #----#
samsung galaxy s2 plus i9105 pilkai m?lynas #----# 739 #----#
samsung phone s7562 galaxy s duos baltas #----# 555 #----#

lg t375 mobile phone black #----# 218 #----#
samsung s6802 galaxy ace duos black #----# 579 #----#
mobilus telefonas samsung galaxy ace onyx black | s5830 #----# 559 #----#


working demo

notice that the code may take a while to parse all the pages, so php may return this error fatal error: maximum execution time of 30 seconds exceeded .... then, simply extend the maximum execution time like this:

ini_set('max_execution_time', 300); //300 seconds = 5 minutes
Saturday, May 29, 2021

if you know the exact string you want to replace you can use str_replace

$string = '<p>this is my super dupa text and it has my phone number: 083 23212212 32</p>';
$string = str_replace("phone number:", "<b>phone number:</b>", $string);

string now has 'phone number:' in bold. if you have a number of things to replace you can use str_replace with arrays. look at the examples on the php website here:

Saturday, May 29, 2021

no, simple html dom doesn't do dom manipulation. with phpquery though you can do:

$doc->find('head')->append('<script src="foo"></script>');
Friday, November 12, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :