XPath - Can you remove commas from node?

I have an xPath request that grabs plaintext from a div as

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

This query returns The text that I want,

Anyway, can I remove this comma in my query?

thank

EDIT:

Using the Mad code below, my query now looks like this (I replaced the ,word in the example helloto make it a little less confusing.)

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

And I am trying to repeat the results of the query as follows:

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

The error I get is

Invalid argument supplied for foreach() in ... on line 50
+3
source share
2 answers

You can use translate()to replace a character with ,an empty string:

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

Although this can lead to a trailing space if the comma was preceded or followed by a space in the text()node.

normalize-space() / :

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

, .

substring-before(), , ,:

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

, ,, substring-before() .

+3

, rtrim - , . , XPath , fn:replace (XPath 2.0).

+2

All Articles