从php中的html字符串获取特定属性
我有一连串的链接,如下所示从php中的html字符串获取特定属性,php,html,Php,Html,我有一连串的链接,如下所示 <link type="image/png" href="http://infinitewp.com/wp-content/themes/mystile-child/images/favicon.png" /> <link rel="shortcut icon" type="image/png" href="http://infinitewp.com/wp-content/themes/mystile-child/images/favicon.p
<link type="image/png" href="http://infinitewp.com/wp-content/themes/mystile-child/images/favicon.png" />
<link rel="shortcut icon" type="image/png" href="http://infinitewp.com/wp-content/themes/mystile-child/images/favicon.png" />
<link type="image/png" href="http://infinitewp.com/wp-content/themes/mystile-child/images/favicon.png" />
这段代码,但我需要得到这个链接的href?如何以简单的方式进行此操作?尝试以下方法:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "Your URL");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec ($ch);
curl_close($ch);
$xml = new DOMDocument();
$xml->strictErrorChecking = FALSE;
$xml->loadHTML($html);
$xpath = new DOMXPath($xml);
$arr = $xpath->query('//link[@rel="shortcut icon"]');
echo $arr[0]['href']; // This is the content of your href.
您是否想过使用jquery获取所需linkUse和xpath的href。它们使得在html文档中查找内容变得微不足道。执行字符串操作太痛苦了。使用
jquery
@xenish你能给我一些建议吗?在你抓到这张桌子后你想做什么value@ThamaraiselvamThangavelneed储存它!首先,我需要知道href@xenish致命错误:在C:\xampp\htdocs\m\index.php第103行的boolean上调用成员函数xpath()会出现错误,然后您的$html不是有效的XML(这就是为什么我们在上一个答案中添加了DOMDocument)。我编辑了答案,将simpleXML更改为DOMDocument。这个类对有效的XML没有那么严格。几乎-查询是DOMXPath
的一种方法,而不是DOMDocument
。。。只需要添加一些东西,比如$xPath=newdomxpath($xml)
然后更改$xml->query(…)
到$xPath->query(…)
您可能还可以在所有curl
内容中添加项目符号,只需使用$xml->loadHTMLFile()
致命错误:无法使用doElement类型的对象作为数组
产生错误
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "Your URL");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec ($ch);
curl_close($ch);
$xml = new DOMDocument();
$xml->strictErrorChecking = FALSE;
$xml->loadHTML($html);
$xpath = new DOMXPath($xml);
$arr = $xpath->query('//link[@rel="shortcut icon"]');
echo $arr[0]['href']; // This is the content of your href.