문제

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