Getting the Inner HTML of a DomElement in PHP [duplicate] - php

This question already has answers here:
How to get innerHTML of DOMNode?
(9 answers)
Closed 5 years ago.
What's the simplest way to get the innerHTML (tags and all) of a DOMElement using PHP's DOM functions?

$html = '';
foreach($parentElement->childNodes as $node) {
$html .= $dom->saveHTML($node);
}
CodePad.

Inner HTML
Try approach suggested by #trincot:
$html = implode(array_map([$node->ownerDocument,"saveHTML"], iterator_to_array($node->childNodes)));
Outer HTML
Try:
$html = $node->ownerDocument->saveHTML($node);
or in PHP lower than 5.3.6:
$html = $node->ownerDocument->saveXML($node);

Related

php replace id element content in string [duplicate]

This question already has answers here:
PHP DOMDocument replace content by div id
(1 answer)
How do you parse and process HTML/XML in PHP?
(31 answers)
Find an element by id and replace its contents with php
(3 answers)
Change innerHTML of a php DOMElement [duplicate]
(10 answers)
DOM change element content
(1 answer)
Closed 3 years ago.
a bit stuck, is it possible to use PHP to update/change html element contents.
So replace only 'Hello World' in the string below based off the the id name.
$html = '<h1 id="item" class="abc" data="efg">Hello World</h1>';
Perhaps using preg_replace, just want to leave all other content in there, eg. class, data, etc.
$html = preg_replace('<h1 id="item">????</h1>', 'New Content', $html);
Thanks
May be below code will help
$html = '<h1 id="item" class="abc" data="efg">Hello World</h1>';
$elementId = "item";
$newString = "Replace World";
$dom = new DOMDocument();
$dom->loadHTML($html);
$belement = $dom->getElementById("$elementId");
$oldString = $belement->nodeValue;
$newHTML = str_replace("$oldString","$newString","$html");
echo $newHTML;
Note: Please change $elementId and $newString with original values

dom x path to grab href value [duplicate]

This question already has answers here:
How to extract a node attribute from XML using PHP's DOM Parser
(3 answers)
Closed 3 years ago.
I have the following html
<div class="logo">***® text.<sup>TM</sup></div>
I would like to get the value of href with php dom xpath, how would I accomplish that?
This is what I have tried:
$anchors = $domXpath->query("//div[#class='logo']/a");
foreach($anchors as $a)
{
print $a->nodeValue." - ".$a->getAttribute("href")."<br/>";
}
Here is the solution.
$xpath = new DOMXpath($dom);
$link = $xpath->query('//div[#class="logo"]/a');
$link->getAttribute('href')

String between php [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 6 years ago.
I've got something like this:
$string = '<some code before><div class="abc">Something written here</div><some other code after>'
What I want is to get what is within the div and output it:
Something written here
How can I do that in php? Thanks in advance!
You would use the DOMDocument class.
// HTML document stored in a string
$html = '<strong><div class="abc">Something written here</div></strong>';
// Load the HTML document
$dom = new DOMDocument();
$dom->loadHTML($html);
// Find div with class 'abc'
$xpath = new DOMXPath($dom);
$result = $xpath->query('//div[#class="abc"]');
// Echo the results...
if($result->length > 0) {
foreach($result as $node) {
echo $node->nodeValue,"\n";
}
} else {
echo "Empty result set\n";
}
Read up on the expression syntax for XPath to customize your DOM searches.

How to get html code of DOMElement node? [duplicate]

This question already has answers here:
Closed 10 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I have this html code:
<html>
<head>
...
</head>
<body>
<div>
<div class="foo" data-type="bar">
SOMECONTENTWITHMORETAGS
</div>
</div>
</body>
I already can get the "foo" element (but only its content) with this function:
private function get_html_from_node($node){
$html = '';
$children = $node->childNodes;
foreach ($children as $child) {
$tmp_doc = new DOMDocument();
$tmp_doc->appendChild($tmp_doc->importNode($child,true));
$html .= $tmp_doc->saveHTML();
}
return $html;
}
But I'd like to return all html tags (including its attributes) of DOMElement. How I can do that?
Use the optional argument to DOMDocument::saveHTML: this says "output this element only".
return $node->ownerDocument->saveHTML($node);
Note that the argument is only available from PHP 5.3.6. Before that, you need to use DOMDocument::saveXML instead. The results may be slightly different. Also, if you already have a reference to the document, you can just do this:
$doc->saveHTML($node);
PHP Simple HTML DOM Parser should do the job!

php DOMDocument How to convert node value to string [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How can I get an element's serialised HTML with PHP's DOMDocument?
PHP + DOMDocument: outerHTML for element?
I am trying to extract all img tags from a string. I am using:
$domimg = new DOMDocument();
#$domimg->loadHTML($body);
$images_all = $domimg->getElementsByTagName('img');
foreach ($images_all as $image) {
// do something
}
I want to put the src= values or even the complete img tags into an array or string.
Use saveXML() or saveHTML() on each node to add it to an array:
$img_links = array();
$domimg = new DOMDocument();
$domimg->loadHTML($body);
$images_all = $domimg->getElementsByTagName('img');
foreach ($images_all as $image) {
// Append the XML or HTML of each to an array
$img_links[] = $domimg->saveXML($image);
}
print_r($img_links);
You could try a DOM parser like simplexml_load_string. Take a look at a similar answer I posted here:
Needle in haystack with array in PHP

Categories