Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中使用xpath和dom获取值_Php_Html_Dom_Xpath - Fatal编程技术网

在php中使用xpath和dom获取值

在php中使用xpath和dom获取值,php,html,dom,xpath,Php,Html,Dom,Xpath,这是我要分析的html部分: <center> <table border="0" cellpadding="0" cellspacing="0" > <td> Some text <br> <font color=brown>label0</font>Value0 <br> <font color=brown>Label1</font>Value1.<br> Some te

这是我要分析的html部分:

<center>
<table border="0" cellpadding="0" cellspacing="0"  >
<td>
Some text
<br>
<font color=brown>label0</font>Value0
<br>
<font color=brown>Label1</font>Value1.<br>
Some text
<br>

<font color=brown>Label2</font>Value2<br>
</td>
</table>

</center>
其中元素是对
../font
的查询,我得到了标签0,1,2。但我想要价值。怎么做?如果有必要,我还可以提供更多的代码

$dom = new DOMDocument();
$xpath = new DOMXPath($dom);    
$xpath->loadHTML('link/to/html');
$fonts = $xpath->query('font');
foreach ($fonts as $font){
    echo $font->nextSibling->nodeValue;
}
上面的脚本将回显
中的所有值

但是,你为什么不使用
CSS

尝试一下:

$index=$element->nextSibling->nodeValue;
要获取字体节点后的节点值,请尝试以下操作:

$str = '<center>
<table border="0" cellpadding="0" cellspacing="0"  >
<td>
Some text
<br>
<font color=brown>label0</font>Value0
<br>
<font color=brown>Label1</font>Value1.<br>
Some text
<br>

<font color=brown>Label2</font>Value2<br>
</td>
</table>
</center>';

$dom = new DOMDocument();
$dom->loadHTML($str);
$dom->preserveWhiteSpace = false;
$dom->validateOnParse = true;

$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//font");
foreach ($nodes as $node) {
    echo trim( $node->nextSibling->nodeValue ) . "\n";
}
$str='1〕
一些文本

label0Value0
Label1Value1.
一些文本
Label2Value2
'; $dom=新的DOMDocument(); $dom->loadHTML($str); $dom->preserveWhiteSpace=false; $dom->validateOnParse=true; $xpath=newdomxpath($dom); $nodes=$xpath->query(“//font”); foreach($node作为$node){ 回显修剪($node->nextSibling->nodeValue)。“\n”; }
希望这有帮助。


<center>
<table border="0" cellpadding="0" cellspacing="0"  >
<td>
Some text
<br>
<font color=brown>label0</font><span>Value0</span>
<br>
<font color=brown>Label1</font><span>Value1.</span><br>
Some text
<br>

<font color=brown>Label2</font>Value2<br>
</td>
</table>
</center>
一些文本
label0Value0
Label1Value1.
一些文本
Label2Value2
尝试像上面一样放置span,而不是查询span的字体查询。。。
希望它能有所帮助。

检查您的查询是否只匹配根目录中的
字体。删除
/
,以获取整个文档中的所有
字体。@Jelmer您甚至试过运行代码吗。它完全符合OP的要求,并且符合发布的html结构。我完全了解
/
的功能。首先尝试修正你的答案,这意味着CSS的使用和什么都没有。它声明“//”用于从根元素开始。在本例中,
是,但如果您再次查看该问题。他说这是我想解析的html的一部分,换句话说,
不是根,我还没有创建这个站点。我刚被告知要从中解析一些值,所以我必须处理当前的格式。@ghostrider啊,好的。谢谢回复:)
<center>
<table border="0" cellpadding="0" cellspacing="0"  >
<td>
Some text
<br>
<font color=brown>label0</font><span>Value0</span>
<br>
<font color=brown>Label1</font><span>Value1.</span><br>
Some text
<br>

<font color=brown>Label2</font>Value2<br>
</td>
</table>
</center>