PHP简单的HTML DOM解析器,在没有类和id的标记中查找文本

PHP简单的HTML DOM解析器,在没有类和id的标记中查找文本,php,html,dom,html-parsing,Php,Html,Dom,Html Parsing,我有一个 具体而言,在这些部分: <b>Additive Error:</b> <p> Additive error is the error that is added to the true value and does not depend on the true value itself. In other words, the result of the measurement is considered as a sum of the true

我有一个

具体而言,在这些部分:

<b>Additive Error:</b>
<p> Additive error is the error that is added to the true value and does not 
depend on the true value itself. In other words, the result of the measurement is 
considered as a sum of the true value and the additive error:   </p> 
我试图将foreach更改为:
foreach($html->find('bp')为$e){

然后
foreach($html->find('/bp')as$e){

然后它只给我一页空白。 我做错了什么?
谢谢。

如果您想要b或p标记中的所有内容,只需执行
foreach($html->find('b,p')as$e){…}
为什么不使用PHP内置的DOM扩展和xpath

libxml_use_internal_errors(true);  // <- you might needs this if that page has errors
$dom = new DomDocument();
$dom->loadHtml($curl_scraped_page);
$xpath = new DomXPath($dom);
print $xpath->evaluate('string(//p[preceding::b]/text())');
//                             ^
//  this will get you text content from <p> tags preceded by <b> tags
要将它们全部作为
DOMNodeList
,请使用
string()
函数:
//p[previous::b]/text()
,然后您可以迭代列表并访问每个节点的
textContent
属性…

试试这个

<?php
$dom = new DOMDocument();
@$dom->loadHTMLFile('http://www.statistics.com/index.php?page=glossary&term_id=703');
$xpath = new DOMXPath($dom);

$mytext = '';
foreach($xpath->query('//font') as $font){
    $mytext =  $xpath->query('.//p', $font)->item(0)->nodeValue;
    break;
}

echo $mytext;
?>


不,我只想要上面p标签内的文本,只有那一个..我该怎么办?如果你只想要那一个,我怀疑你可能有点完蛋了。我会帮你,但我不知道怎么做。是的,你是对的。我完蛋了,:(我做这件事已经很长时间了,但是我的代码一直失败。你认为有可能做到吗?可能,但我不知道怎么做。你可以根据一个技巧小马的(很好)查找前面有b标记的p标记。)解决方案,但您总是要冒返回多个段落的风险。是的,我只是看了一下。感谢上帝,也感谢您。:)幸运的是,每个具有不同术语的链接都只有一个b标记,后面跟着p标记。这样就可以了,不是吗?我只想要上面p标记中的文本,只有那一个。我该怎么办?哦,天哪,你救了我的命!非常感谢你。再次感谢。嘿,我还有一个问题。我想从其他页面进行一些解析,但我读到我们不能在删除前一个对象之前创建新对象。我的问题是:在创建一些
simple\u html\u dom
对象之前,如何删除$dom?谢谢,通过为变量分配一个新对象,例如
$dom=new DomDocument()
…但是为什么使用“simple\u html\u dom”而不是直接使用DomDocument?
libxml_use_internal_errors(true);  // <- you might needs this if that page has errors
$dom = new DomDocument();
$dom->loadHtml($curl_scraped_page);
$xpath = new DomXPath($dom);
print $xpath->evaluate('string(//p[preceding::b]/text())');
//                             ^
//  this will get you text content from <p> tags preceded by <b> tags
string((//p[preceding::b]/text())[1])
<?php
$dom = new DOMDocument();
@$dom->loadHTMLFile('http://www.statistics.com/index.php?page=glossary&term_id=703');
$xpath = new DOMXPath($dom);

$mytext = '';
foreach($xpath->query('//font') as $font){
    $mytext =  $xpath->query('.//p', $font)->item(0)->nodeValue;
    break;
}

echo $mytext;
?>