无法使用XPATH和PHP操作/兄弟之后的文本

时间:2015-10-08 16:02:28

标签: php xpath

您好我试图在使用XPATH和PHP表达后操作文本,但不起作用。

->query('//th[contains(., "Code")]/following-sibling::*[1]')

结果:

111,222,333

预期:

222,333

或者完美会在阵列中爆炸以进一步操纵

[111]
[222]
[333]

我没有成功地尝试过这个:

->query('//th[contains(., "Code")]/following-sibling::*[substring-after(.,",")]')

->query('substring-after(/th[contains(., "Code")]/following-sibling::*[1],",")')

->query('//th[contains(., "Code")]/substring-after(/following-sibling::*[1],",")')

有什么帮助解决这个问题吗?非常感谢

2 个答案:

答案 0 :(得分:0)

那一定对你有用

substring-after(/tr/th[.='Code']/following-sibling::td, ',')

您可能需要的小改进(删除前后空格)

normalize-space(substring-after(/tr/th[.='Code']/following-sibling::td, ','))

答案 1 :(得分:0)

使用susbstring-after()函数的xpath表达式看起来很有希望。但请注意DOMXPath::query()无法返回节点集以外的其他内容。要执行不返回节点集的xpath表达式,即substring-after()函数,请改用DOMXPath::evaluate()

$xpath->evaluate('substring-after(//th[contains(., "Code")]/following-sibling::*[1],",")');