잘 모르겠지만 두 번째 XPath식이 이미 설명하고있는 것을 수행합니다. 그것은 a 요소의 텍스트 노드에 대해 일치하지만, href 속성하지 않습니다
$html = <<< HTML
<ul>
<li>
<a href="http://example.com/page?foo=bar">Description</a>
</li>
<li>
<a href="http://example.com/page?lang=de">Description</a>
</li>
</ul>
HTML;
$xml = simplexml_load_string($html);
$list = $xml->xpath("//a[contains(@href,'foo')]");
출력 :
array(1) {
[0]=>
object(SimpleXMLElement)#2 (2) {
["@attributes"]=>
array(1) {
["href"]=>
string(31) "http://example.com/page?foo=bar"
}
[0]=>
string(11) "Description"
}
}
당신이 볼 수 있듯이, 반환 NodeList를 함유 HREF 만 a 요소를 포함 foo (나는 당신이 찾고있는 것을 이해합니다). XPath가 으로 변환되기 때문에 전체 요소를 경합합니다. foo를 포함하는 href 속성을 가진 A 요소를 모두 가져옵니다.. 당신은 단지 속성 자체를 반환하려면 그런 다음 SimpleXML을,이 비록 SimpleXML을 요소를 반환 할 것이라고
//a[contains(@href,'foo')]/@href
참고해야 할 것,
echo $list[0]['href'] // gives "http://example.com/page?foo=bar"
와 속성에 액세스 것입니다 :
array(1) {
[0]=>
object(SimpleXMLElement)#3 (1) {
["@attributes"]=>
array(1) {
["href"]=>
string(31) "http://example.com/page?foo=bar"
}
}
}
하지만 당신은
echo $list[0] // gives "http://example.com/page?foo=bar"
에 의해 현재 출력 URL을 수3210
예, 'needle'을 검색하면 원하는 * http : //example.com? param = needle * ...이 아닌 * $ node-> nodeValue(); *에 텍스트 부분이 반환됩니다. – MattW