2008 ...">

Fix XPath query to extract inner text div

I am using XPath to query into an HTML document where there is the following div:

<div class="h1">2008 Honda Accord Coupe<small> — Wuse II</small></div> 

I wanted to get only the internal text <div class="h1">2008 Honda Accord Coupe and not include the <small>inner text</small>

I am making the following XPath request: //div[@class='h1'] , which definitely returns the integer <div>...</div> node.

How can I get only a part without <small>...</small>

thanks

+8
dom html xpath
source share
2 answers

Use the text() function:

 //div[@class='h1']/text() 

Tested in phpFiddle

+10
source share

You can do this:

 $name = trim($xpath->query('//div[@class="h1"]')->item(0)->childNodes->item(0)->nodeValue); var_dump($name); 
0
source share

All Articles