使用 simplehtmldom 尝试查找没有 out 和 id 或类的 URL


Using simplehtmldom trying to find a URL with out and id or class

第一次在这里张海报,做了大约几个小时的搜索和尝试,但卡住了......所以对我放轻松:)

有一个包含这个的页面...

<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>

我正在尝试获取文档中的网址 http://www.mywebsite.com.li 标签的位置。

唯一

唯一要关闭的独特且恒定的东西是 span 标签中的"访问网站"文本。 有没有办法找到它并从 onclick 事件转到 document.location 属性的父 li 标签?

任何帮助将不胜感激!!

谢谢

莫先生。

当然,将其加载到SimpleHTMLDOM对象中,然后仅使用它定位<li>标签。定位 onclick="" 属性以获取其中的值。

免责声明:我不是正则表达式专家。

$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;
    }
}