[英]How to get link inside tag a html (html as plain text) in PHP?
I have the following code 我有以下代码
$input = '<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<a class="class1 class2" href="link-to-web1.html">Link</a>
<a class="class1 class2" href="abc/link-to-web1.html">Link</a>
<a class="class1 class2" href="abc/xyz/link-to-web1.html">Link</a>
<a class="another" href="abc/xyz/link.html">Link</a>
</body>
</html>';
I got this string after using curl. 我使用curl后得到了这个字符串。 Then I wanna get all link (href link) of all a tag with class 'class1 class2', how to do that?
然后我想获取所有带有“ class1 class2”类标签的所有链接(href链接),该怎么做? I had tried some method but it's not work :(
我试过一些方法,但是不行:(
Use the DOMDocument 使用DOMDocument
$dom = new DOMDocument;
// load your html
$dom->loadHTML($input);
// loop all the anchor tags
foreach ($dom->getElementsByTagName('a') as $a) {
// check the calss
if($a->getattribute('class') == 'class1 class2') {
// echo href
echo $a->getattribute('href')."<br/>";
}
}
Out put: 输出:
link-to-web1.html
abc/link-to-web1.html
abc/xyz/link-to-web1.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.