Pergunta

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?

Foi útil?

Solução

You need to call saveHTML on the ownerDocument property:

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

Outras dicas

$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()
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top