Get div's HTML content via xpath [duplicate]

2020-03-25 11:32发布

I need a support on the following issue, I have a website where I want to get a product descriptions via xpath. Below the example html of the content:

<div id="description">
 <span>some test</span>
 <img src="some src">
 <p>
  some content etc.
 </p>
</div>

and now I use the below code:

$result = $xpath->query('//div[@id="description"]');

but when I'm outputting the result with the below line:

echo "<pre>".$result->item( 0 )->nodeValue."</pre>"; // there is always only one element

what I get is only unformatted: "some testsome content etc." with no spaces and other tags. I know it is because xpath treats all the other tags as nodes but...

I there a way to get the div's content as HTML ?

标签: php xpath
2条回答
Bombasti
2楼-- · 2020-03-25 11:49

DOM Document is SLOOOOOW

Why cant you do this with a regular expression?

i would do this:

// put everything on one line $content = preg_replace("!\r\n!", "", $content);

preg_match("!(.*?)!", $content, $matches);

the above will search for and then takes everything up until the first

Based on the HTMl above this will work. but not if you have other tags within the HTML

查看更多
啃猪蹄的小仙女
3楼-- · 2020-03-25 11:54

Hey I did something similar a while back and this post helped me:

PHP DOMDocument / XPath: Get HTML-text and surrounded tags

It is a bit of overhead in your code, I mean if you thought (like me) it could be solved in one line with a different XPath query or something...

Hope it helped

查看更多
登录 后发表回答