简体   繁体   中英

How to get elements between two tags Simple Html Dom

This is my Html

<b><font color="Red">Flash Player 720p HD Quality Online Links</font></b>
        <br>
        <br>
        <a href="http://bestarticles.me/jaana-na-dil-se-door/?si=5325359" target="_blank">Jaana Na Dil Se Door 6th February 2017 Watch Online Video- Part 1</a>
        <br>
        <a href="http://bestarticles.me/jaana-na-dil-se-door/?si=5325360" target="_blank">Jaana Na Dil Se Door 6th February 2017 Watch Online Video- Part 2</a>
        <br>
        <br>
        <b><font color="Red">Dailymotion 720p HD Quality Online Links</font></b>
        <br>
        <br>
        <a href="http://bestarticles.me/jaana-na-dil-se-door/?si=k4r2rHPOgem8yAlGqjj" target="_blank">Jaana Na Dil Se Door 6th February 2017 Watch Online Video- Part 1</a>
        <br>
        <a href="http://bestarticles.me/jaana-na-dil-se-door/?si=k63MLC2Vq6fxsPlGqjp" target="_blank">Jaana Na Dil Se Door 6th February 2017 Watch Online Video- Part 2</a>
        <br>
        <br>
        <b><font color="Red">TVLogy 720p HD Quality Online Links</font></b>
        <br>
        <br>
        <a href="http://reviewtv.in/star-plus/?si=YD29025" target="_blank">Jaana Na Dil Se Door 6th February 2017 Watch Online Video- Part 1</a>
        <br>
        <a href="http://reviewtv.in/star-plus/?si=YD29026" target="_blank">Jaana Na Dil Se Door 6th February 2017 Watch Online Video- Part 2</a>
        <br>
        <br>
        <b><font color="Red">Letwatch 720p HD Quality Online Links</font></b>
        <br>
        <br>
        <a href="http://www.tellycolors.me/star-plus/?si=j3vpekz3jeiv" target="_blank">Jaana Na Dil Se Door 6th February 2017 Watch Online Video - Part 1</a>
        <br>
        <a href="http://www.tellycolors.me/star-plus/?si=bdjg53bz9gdi" target="_blank">Jaana Na Dil Se Door 6th February 2017 Watch Online Video - Part 2</a>
        <br>
        <br>
        <b><font color="Red">Vidwatch 720p HD Quality Online Links</font></b>
        <br>
        <br>
        <a href="http://hd-rulez.info/vidwatch.php?id=73sbn356g9nc" target="_blank">Jaana Na Dil Se Door 6th February 2017 Watch Online Video - Part 1</a>
        <br>
        <a href="http://hd-rulez.info/vidwatch.php?id=73x796cifyvq" target="_blank">Jaana Na Dil Se Door 6th February 2017 Watch Online Video - Part 2</a>
        <br>
        <br>

I am using Simple Html Dom php library for scrapping. I want to scrap <b> tags with their anchor tags. Every <b> elements have their <a> anchor set. so I want to scrap like this

array(
       'Flash Player' => array( 'link1', 'link2' ),
       'Daiylymotion' => array('link1', 'link2', 'link3'),
       etc...
);

And this is what I am doing. First I escaped all <br> tags then loop on all <b> tags then I am trying to get next siblings of <b> tag by $b->next_sibling() but its not working because by escaping <br> tags index of elements not updated. Here is my code

$html = str_get_html($html);
$content = $html->find('div.postcontent',0);

   //escape all br
    foreach($content->find('br') as $br){
        $br->outertext = '';
    }

    foreach($content->find('b') as $key => $b){


        echo $b->plaintext;

    }

Please help me by scrapping <b> with their <a> tags with another strategy. thank you

I don't know if there is any other easier methods. But this code will give your desired output as long as each <b> tag has exactly two <a> tags following.

    $aCount = 0;
    $result = array();
    foreach($content->find('b') as $key => $b){
        $index = $b->plaintext;  
        for($i=0;$i<2;$i++){
            $result[$index][] = $content->find('a',$aCount++)->href;
        }       
    }  
    print_r($result);

The output will be like

Array
(
    [Flash Player 720p HD Quality Online Links] => Array
        (
            [0] => http://bestarticles.me/jaana-na-dil-se-door/?si=5325359
            [1] => http://bestarticles.me/jaana-na-dil-se-door/?si=5325360
        )

    [Dailymotion 720p HD Quality Online Links] => Array
        (
            [0] => http://bestarticles.me/jaana-na-dil-se-door/?si=k4r2rHPOgem8yAlGqjj
            [1] => http://bestarticles.me/jaana-na-dil-se-door/?si=k63MLC2Vq6fxsPlGqjp
        )

    [TVLogy 720p HD Quality Online Links] => Array
        (
            [0] => http://reviewtv.in/star-plus/?si=YD29025
            [1] => http://reviewtv.in/star-plus/?si=YD29026
        )

    [Letwatch 720p HD Quality Online Links] => Array
        (
            [0] => http://www.tellycolors.me/star-plus/?si=j3vpekz3jeiv
            [1] => http://www.tellycolors.me/star-plus/?si=bdjg53bz9gdi
        )

    [Vidwatch 720p HD Quality Online Links] => Array
        (
            [0] => http://hd-rulez.info/vidwatch.php?id=73sbn356g9nc
            [1] => http://hd-rulez.info/vidwatch.php?id=73x796cifyvq
        )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM