Jump to content

Gathering Print Outs Of URL Links In <DIV>?


happyhippy

Recommended Posts

foreach($html->find('a') as $element)
       echo $element->href . '<br>';

 

I understand that the code above results in printing out all links on the page in text. I want the same but only in a <div> I choose. See below:

 

<div id=web>
<A href = myweb.com/url1>home</a>
<A href = myweb.com/url2>about</a>
<A href = myweb.com/url3>contact</a>
</div>

 

I want for example <div id=web> to print out onto page the urls in plain text from within that <div> or put into an array so I can create variables out of each url to do different actions.

 

I'm using simple html dom

 

I've tried different ways but I either get an error, a blank white page, or a print out of the word array?  :confused:

 

Either direct me to some web sources or write a code out for me, thanks

Link to comment
https://forums.phpfreaks.com/topic/246089-gathering-print-outs-of-url-links-in/
Share on other sites

Something like this?

preg_match_all('/\<div id=web\>(.*?)\<\/div\>/is', $page, $matches);

where $page contains the whole page's code. $matches will be created as an array containing the results. (not sure if you also need to escape the = sign).

Try this :

 

foreach($html->find('div[id=web]') as $div) {
     foreach($div->find('a') as $element) {
        echo $element->href . '<br>';
     }
}
//OR
$div = $html->find('div[id=web]');
foreach($div->find('a') as $element) {
        echo $element->href . '<br>';
} 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.