PHP简单HTML DOM解析器查找字符串
我使用的是PHP简单DOM解析器,但它似乎没有搜索文本的功能。我需要搜索一个字符串并找到它的父id。基本上与正常用法相反 有人知道怎么做吗PHP简单HTML DOM解析器查找字符串,php,html,dom,Php,Html,Dom,我使用的是PHP简单DOM解析器,但它似乎没有搜索文本的功能。我需要搜索一个字符串并找到它的父id。基本上与正常用法相反 有人知道怎么做吗 $d = new DOMDocument(); $d->loadXML($xml); $x = new DOMXPath($d); $result = $x->evaluate("//text()[contains(.,'617.99')]/ancestor::*/@id"); $unique = null; for($i = $result-&
$d = new DOMDocument();
$d->loadXML($xml);
$x = new DOMXPath($d);
$result = $x->evaluate("//text()[contains(.,'617.99')]/ancestor::*/@id");
$unique = null;
for($i = $result->length -1;$i >= 0 && $item = $result->item($i);$i--){
if($x->query("//*[@id='".addslashes($item->value)."']")->length == 1){
echo 'Unique ID is '.$item->value."\n";
$unique = $item->value;
break;
}
}
if(is_null($unique)) echo 'no unique ID found';
得到了答案。整个示例有点长,但它是有效的。我还显示了输出 我们将要查看的HTML:
<html>
<head>
<title>Simple HTML DOM - Find Text</title>
</head>
<body>
<h3>Simple HTML DOM - Find Text</h3>
<div id="first">
<p>This is a paragraph inside of div 'first'.
This paragraph does not have the text we are looking for.</p>
<p>As a matter of fact this div does not have the text we are looking for</p>
</div>
<div id="second">
<ul>
<li>This is an unordered list.
<li id="love1">We are looking for the following word love.
<li>Does not contain the word.
</ul>
<p id="love2">This paragraph which is in div second contains the word love.</p>
</div>
<div id="third">
<a id="love3" href="goes.nowhere.com">link to love site</a>
</div>
</body>
</html>
他们写的就这些 想象一下,任何标记都有一个“纯文本”属性,并使用标准属性选择器 因此,HTML:
<div id="div1">
<span>London is the capital</span> of Great Britain
</div>
<div id="div2">
<span>Washington is the capital</span> of the USA
</div>
伦敦是英国的首都
华盛顿是美国的首都
可以想象为:
<div id="div1" plaintext="London is the capital of Great Britain">
<span plaintext="London is the capital ">London is the capital</span> of Great Britain
</div>
<div id="div2" plaintext="Washington is the capital of the USA">
<span plaintext="Washington is the capital ">Washington is the capital</span> of the USA
</div>
伦敦是英国的首都
华盛顿是美国的首都
解决您的任务的PHP只是:
<?php
$t = '
<div id="div1">
<span>London is the capital</span> of Great Britain
</div>
<div id="div2">
<span>Washington is the capital</span> of the USA
</div>';
$html = str_get_html($t);
$foo = $html->find('span[plaintext^=London]');
echo "ID: " . $foo[0]->parent()->id; // div1
?>
(请记住,
标记的“明文”是用空格符号填充的;这是简单HTML DOM的默认行为,由常量default\u SPAN_TEXT
定义)这是PHP的DOMDocument
,而不是OP所说的那样。Ack,错过了这一点。我仍然无法让我的头脑绕过那些使用慢,慢的东西的人,但是你是对的,这不是OP当时想要的答案。当然,在加载之前,设置$d->recover=true$d->StrightErrorChecking=false当然,对于HTML,使用loadHTML()
而不是loadXML()
。如果仍然有很多错误,您不能忽略这些错误(在生产站点上永远不要显示错误),您可以设置libxml\u-use\u-internal\u-errors(true)
将它们与其他PHP错误分开处理。Ack,包装器
不是我们想要的:)。糟糕的是,我的XPath
有点生疏,试试//text()[contains(,'617.99')]/parent:*/@id
,似乎在这里工作。可以通过预先添加@
(@$d->loadHTML($html)
,这有点邪恶,或者使用libxml\u使用内部错误($d->loadHTML($html);libxml\u清除错误(
(首选IMHO)。id应该是唯一的,但我们都知道它有时不是唯一的。您可以使用$x->query(“/*[@id='theid'])->length==1
(对于priceIncTaxSpan3047
它是唯一的,但请查看50表,难怪:)$e->id
是获取id属性的简单DOM方法。也许可以尝试将$eles=$html->find('*');
更改为$eles=$html->find('p,div');
或其他什么。它不是getAttribute('id')吗…无论如何,我都无法让它工作:SGreat示例。您知道如何从文本返回到元素吗?我想按文本搜索,然后找到最近的元素。它来自一个没有任何类或ID的旧表布局。到目前为止,这是最好的答案
<div id="div1">
<span>London is the capital</span> of Great Britain
</div>
<div id="div2">
<span>Washington is the capital</span> of the USA
</div>
<div id="div1" plaintext="London is the capital of Great Britain">
<span plaintext="London is the capital ">London is the capital</span> of Great Britain
</div>
<div id="div2" plaintext="Washington is the capital of the USA">
<span plaintext="Washington is the capital ">Washington is the capital</span> of the USA
</div>
<?php
$t = '
<div id="div1">
<span>London is the capital</span> of Great Britain
</div>
<div id="div2">
<span>Washington is the capital</span> of the USA
</div>';
$html = str_get_html($t);
$foo = $html->find('span[plaintext^=London]');
echo "ID: " . $foo[0]->parent()->id; // div1
?>