PHP Dom Get Nodevalue HTML? (Without Stripping Tags)

PHP DOM get nodevalue html? (without stripping tags)

I have never done what you're attempting to do, but as a stab in the dark, using the API docs, does echo $entry->textContent; work?

Adding an update. This is from the comments located on the docs page for DOMNode:

Hi!

Combining all th comments, the easiest way to get inner HTML of the node is to use this function:

<?php  function get_inner_html( $node ) { 
$innerHTML= '';
$children = $node->childNodes;
foreach ($children as $child) {
$innerHTML .= $child->ownerDocument->saveXML( $child );
}

return $innerHTML; } ?>

Or, maybe a simpler method is just to do:

echo $domDocument->saveXML($entry);

PHP nodevalue stripping html tags

You need to call saveHTML on the ownerDocument property:

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

Php DOM: when setting nodeValue, code is not formatted, literal HTML tags get displayed instead

In a tag with content like: <h2>Tom</h2>, Tom is the nodeValue and h2 is the nodeName.

You cannot write to nodeName. To create a new node you will have to use this:

$html = new DOMDocument();
$html->loadHTML($content);

$elements = $html->getElementsByTagName('div');
foreach($elements as $element) {
if ($element->getAttribute('name') == "left_0") {
$newElement = $html->createElement('h2','Tom');
$element->appendChild($newElement);
}

If you want to append nested tags, like <p><h2>Title</h2></p> you would do:

$paragraph = $html->createElement('p');         // create outer <p> tag
$currentElement->appendChild($paragraph); // attach it to parent element
$heading2 = $html->createElement('h2','Title'); // create inner <h2> tag
$paragraph->appendChild($heading2); // attach that to the <p> tag

PHP nodeValue strips html tags - innerHTML alternative?

Since $edits[$i] is a string, you need to parse it into a DOM structure and replace the original content with the new structure.

Update

The code fragment below does an incredible job when using non-XML compliant HTML. (e.g. HTML 4/5)

for($i=0; $i<$num_edits; $i++)
{
$f = new DOMDocument();
$edit = mb_convert_encoding($edits[$i], 'HTML-ENTITIES', "UTF-8");
$f->loadHTML($edit);
$node = $f->documentElement->firstChild;
$entries->item($i)->nodeValue = "";
foreach($node->childNodes as $child) {
$entries->item($i)->appendChild($doc->importNode($child, true));
}
}

PHP DOM: Get NodeValue excluding the child nodes

Hope this will help you out..

Try this code snippet here

<?php
ini_set('display_errors', 1);
$string='<html><body><b>AMZN 466.00 ( 15743 ) ( <span class=\'red\'> -1 </span>)
MSFT 290.00 ( 37296 ) ( <span class=\'red\'> -2 </span>)
TWTR 4,000.00 ( 20 ) ( <span class=\'\'> 0 </span>)</b></body></html>';

$dom = new DOMDocument();
$dom->loadHTML($string);
$dom->getElementsByTagName("b");

$xpath= new DOMXPath($dom);
$result=$xpath->query("//b/span");//here we are querying domdocument to find span which is inside b.

$nodesToRemove=array();//here we are maintaining an array of nodes which we want to remove
foreach($result as $node)
{
$node->parentNode->removeChild($node);//removing nodes from its parent
}
echo $dom->getElementsByTagName("b")->item(0)->textContent;//displaying content after removing nodes.

Output:

AMZN 466.00 ( 15743 ) ( ) 
MSFT 290.00 ( 37296 ) ( )
TWTR 4,000.00 ( 20 ) ( )

PHP DOM: Get Nodevalue without descendant nodes

As Phil mentioned text is also organized in nodes. Therefore your node inhalt has two children: A text node ("123") and an element node named more, which also has a text node ("45").

If you just want to know, if there is text before the element more, test if there is at least one element and then test if the first element is text (should be DOMText as far as I remember).

If you like to know, if there is at least one element "at the root", iterate over all children and make the same tests.

http://www.php.net/manual/en/class.domtext.php



Related Topics



Leave a reply



Submit