0

感谢您的帮助!

我试图弄清楚如何从包含单个标签的 php 变量中提取 href 属性......就像这样

$var = "<a class='coupon_link' href='http://www.anrdoezrs.net/r365wktqks7EDD8BDB799AACCEE' target='_blank' onmouseover='window.status=' http:='' www.myjewelrybox.com';return='' true;'='' onmouseout='window.status=' ';return=''><img src='http://www.lduhtrp.net/fl115vvzntrCJIIDGIGCEEFFHHJJ' alt='' border='0'></a>";

我听说你可以用 xpath 以某种方式做到这一点,但我的尝试没有成功:-/

这是我正在尝试的:

  $xml  = simplexml_load_string($var);
  $myhref = $xml->xpath("//a[contains(@href,'foo')]");

  echo $myhref;

再次感谢!

4

1 回答 1

0

在@Charles 评论之后,我建议在解析随机 HTML 时使用DOMXPath

$var = "<a class='coupon_link' href='http://www.anrdoezrs.net/r365wktqks7EDD8BDB799AACCEE/foo' target='_blank' onmouseover='window.status=' http:='' www.myjewelrybox.com';return='' true;'='' onmouseout='window.status=' ';return=''><img src='http://www.lduhtrp.net/fl115vvzntrCJIIDGIGCEEFFHHJJ' alt='' border='0'></a>";

$xml = new DOMDocument(); 
@$xml->loadHTML($var);
$oXpath = new DOMXPath($xml);
$inputs = $oXpath->query("//a[contains(@href,'foo')]");
var_dump($inputs);
于 2012-12-11T16:48:10.407 回答