Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从php中的html字符串获取特定属性_Php_Html - Fatal编程技术网

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