質問

I have seem similar solutions else where but I haven't been able to convert to work with my own code.

I have a function that splits an html string between the paragraph tags and returns in an array. Code is as follows...

$dom = new DOMDocument();
$dom->loadHTML($string);
$domx = new DOMXPath($dom);
$entries = $domx->evaluate("//p");
$result = array();
foreach ($entries as $entry) {
    $result[] = '<' . $entry->tagName . '>' . $entry->nodeValue .  '</' . $entry->tagName . '>';
}

return $result;

Can someone assist me to remove the nodeValue element from this so it returns the paragraph content with html tags complete?

役に立ちましたか?

解決

You need to call saveHTML on the ownerDocument property:

$result[] = $entry->ownerDocument->saveHTML($entry);

他のヒント

$dom = new DOMDocument();
$dom->loadHTML($string);
$entries = $dom->getElementsByTagName('p');
$new_dom = new DOMDocument();
foreach ($entries as $entry) {
    $new_dom->appendChild($new_dom->importNode($entry, TRUE));
}
$result = $new_dom->saveHTML()
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top