dongtaigan1594 2012-04-15 20:39
浏览 143
已采纳

xPath - 你能从节点中删除逗号吗?

I have an xPath query that grabs plaintext from a div like this

xpath->query("//div[@id='main']//span[@class='meta']/text()")

This query returns The text that I want,

Is there anyway that I can remove that comma within my query?

Thanks

EDIT:

Using Mad's code from below, my query now looks like this (I've replaced the , in the example with the word hello to make it a bit less confusing.)

$search_term = $xpath->query("translate('//div[@id='main']//span[@class='meta']/text()', 'hello', '')");

And I'm attempting to echo the query results like this:

foreach ($search_term as $st) { 
echo $st->nodeValue; }

The error I'm receiving is

Invalid argument supplied for foreach() in ... on line 50
  • 写回答

2条回答 默认 最新

  • douxian4376 2012-04-15 23:41
    关注

    You could use translate() to replace the , character with empty an string:

    xpath->query("translate(//div[@id='main']//span[@class='meta']/text(), ',', '')")
    

    Although, it could result in a trailing space if the comma was preceded or followed by a space in the text() node.

    You could then use normalize-space() to remove leading/trailing whitespace:

    xpath->query("normalize-space(translate(//div[@id='main']//span[@class='meta']/text(), ',', ''))")
    

    Although, it will also normalize sequences of whitespace in the middle of the text into single spaces.

    You could also use substring-before() to select the text preceding ,:

    xpath->query("substring-before(//div[@id='main']//span[@class='meta']/text(), ' ,')")
    

    However, if the string does not contain , then substring-before() will not return anything.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题