开发者

PHP DOMXPath problem

开发者 https://www.devze.com 2023-03-08 10:55 出处:网络
$xpath = new DOMXpath($doc); $res = $xpath->query(\".//*[@id=\'post2679883\']/tr[2]/td[2]/div[2]\");
$xpath = new DOMXpath($doc);
$res = $xpath->query(".//*[@id='post2679883']/tr[2]/td[2]/div[2]");
foreach( $res as $obj ) {
    var_dump($obj->nodeValue);
}

I开发者_运维技巧 need to take all the items in the id with the word "post".

Example:

<div id="post2242424">trarata</div>
<div id="post114525">trarata</div>
<div id="post8568686">trarata</div>

Question number two: I need to get this elements with HTML tags, but $obj->nodeValue returns text without html tags.


You could use the xpath function starts-with to filter the nodes in your XPath if all the nodes you want start with "post". For example;

$xpath->query(".//*[starts-with(@id, 'post')]/tr[2]/td[2]/div[2]");

For the second part, I think has been answered already - PHP DOMDocument stripping HTML tags

0

精彩评论

暂无评论...
验证码 换一张
取 消