Reputation: 331
First time poster on here, did about a couple hours of searching and trying but got stuck... so go easy on me :)
With a page containing this...
<li onclick="javascript:trackClick(14423, 'web'); document.location='http://www.mywebsite.com';">
<img class="listing-control" src="img/url-profile-listings.png" alt="Get Directions" width="51" height="51" style="padding:4px;">
<span id="web14423">Visit Website</span>
</li>
I am trying to get the url http://www.mywebsite.com in the document.location of the li tag.
The only unique and constant thing to key off is the "Visit Website" text in the span tag. Is there any way to find that and go up to the parent li tag to the the document.location property from the onclick event?
Any help would be greatly appreciated!!!
Thanks,
MrMo.
Upvotes: 1
Views: 80
Reputation: 41893
Of course load it in the SimpleHTMLDOM
object, then just target the <li>
tag with it. Target the onclick=""
attribute to get the values inside it.
Disclaimer: I'm not a regex expert in any way.
$html_string = <<<EOT
<li onclick="javascript:trackClick(14423, 'web'); document.location='http://www.mywebsite.com';">
<img class="listing-control" src="img/url-profile-listings.png" alt="Get Directions" width="51" height="51" style="padding:4px;">
<span id="web14423">Visit Website</span>
</li>
EOT;
$html = str_get_html($html_string);
// after loading the html with either str_get_html or file_get_html
foreach($html->find('li') as $list) {
$script = $list->onclick;
preg_match('/document.location\s*=\s*\'(.*?)\';/', $script, $match);
if(!empty($match)) {
$url = $match[1];
echo $url;
}
}
Upvotes: 1