gpt4 book ai didi

php - DOM loadhtml提取节点和子节点

转载 作者:行者123 更新时间:2023-12-03 17:36:11 31 4
gpt4 key购买 nike

我有一个项目列表,需要获取列表标题属性,链接URL和显示的链接文本以及每个列表标签的span值。

<ul>
<li class="testclass" title="Title 1 goes here">
<a href="http://examplelink1.com">List Text 1</a>
<span>Second List Text 1</span>
</li>
<li class="testclass" title="Title 2 goes here">
<a href="http://examplelink2.com">List Text 2</a>
<span>Second List Text 2</span>
</li>
</ul>


如何使用foreach提取每个列表标记及其值(因为之后需要将这些值插入MySQL db(每个值位于不同的db字段中)。

到目前为止,我只能单独购买它们:

<?php
$doc = new DOMDocument();
@$doc->loadHTML($list);
$imageTags = $doc->getElementsByTagName('a');
foreach($imageTags as $tag) {
$link = $tag->getAttribute('href');
echo $link.'<br/>';
}
?>




<?php
$doc = new DOMDocument();
@$doc->loadHTML($list);
$imageTags = $doc->getElementsByTagName('li');
foreach($imageTags as $tag) {
$link = $tag->getAttribute('title');
echo $link.'<br/>';
}
?>


我找到了一个带有xpath的脚本,但是我不知道如何正确地应用它来获取所需的特定值并在MySQL语句中使用它们:

<?php
$dom = new DOMdocument();
@$dom->loadHTML($list);
$xpath = new DOMXPath($dom);
$elements = $xpath->query("//*");
foreach ($elements as $element) {
echo "<p>". $element->nodeName. "</p>";
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue. "<br/>";
}
}
?>

最佳答案

使用DOMXPath::evaluate()。它是ext/dom的一部分,允许您使用XPath表达式从DOM中获取节点和值。

$dom = new DOMDocument();
$dom->loadHtml($html);
$xpath = new DOMXPath($dom);

// use an xpath expression to fetch the li nodes
foreach ($xpath->evaluate('//ul/li[@class="testclass"]') as $li) {
var_dump(
[
// this is a direct attribute of the li node, use dom method
'title' => $li->getAttribute('title'),
// more complex, use an xpath expression
'href' => $xpath->evaluate('string(a/@href)', $li),
// Cast the node to a string to return the text content
'link-text' => $xpath->evaluate('string(a)', $li),
// works for the span, too
'description' => $xpath->evaluate('string(span)', $li)
]
);
}


输出:

array(4) {
["title"]=>
string(17) "Title 1 goes here"
["href"]=>
string(23) "http://examplelink1.com"
["link-text"]=>
string(11) "List Text 1"
["description"]=>
string(18) "Second List Text 1"
}
array(4) {
["title"]=>
string(17) "Title 2 goes here"
["href"]=>
string(23) "http://examplelink2.com"
["link-text"]=>
string(11) "List Text 2"
["description"]=>
string(18) "Second List Text 2"
}

关于php - DOM loadhtml提取节点和子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29105107/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com