从php中的html字符串中获取特定的attr

时间:2015-05-05 14:41:16

标签: 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.png" />

<link  type="image/png" href="http://infinitewp.com/wp-content/themes/mystile-child/images/favicon.png" />

我需要获得href哪个链接有rel="shortcut icon" 我可以使用

找到快捷图标链接
 strpos($content,"shortcut icon")

此代码,但我需要获取此链接的href?如何以简单的方式做到这一点?

1 个答案:

答案 0 :(得分:0)

试试这个:

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