Frage

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?

War es hilfreich?

Lösung

You need to call saveHTML on the ownerDocument property:

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

Andere Tipps

$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()
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top