使用PHP从类中提取href?

这甚至可能...... 假设我有一些带有“点击”类链接的文字:
<p>I am some text, i am some text, i am some text, i am some text
<a class="click" href="http://www.google.com">I am a link</a>
i am some text, i am some text, i am some text, i am some text</p>
使用PHP,获取类名“click”的链接,然后获取href值?     
已邀请:
有几种方法可以做到这一点,最快的方法是使用XPath:
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);

$nodeList = $xpath->query('//a[@class="click"]');
foreach ($nodeList as $node) {
    $href = $node->getAttribute('href');
    $text = $node->textContent;
}
    
实际上你根本不需要让你的生活复杂化:
$string='that html code with links';
// while matches found
while(preg_match('/<a class="click" href="([^"]*)">/', $string, $matches)){
    // print captured group that's actually the url your searching for
    echo $matches[1];
}
    

要回复问题请先登录注册