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
Related
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')
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 7 years ago.
I have a huge HTML page that contains multiple data like this
<td style="font-size:24px;" bgcolor="#F0F0F0" width="60%">
<strong>ID:Full Name:email#email.com:Mobile:Country</strong>
</td>
I want to extract the data between the tags which is ID:Full Name:email#email.com:Mobile:Country
So what would be the regex or any custom PHP Function?
PS: The above code is being repeated multiple times in a page and I want all that data to be stored in an array.
As of others has said, you can just use DOMDocument and DOMXpath. Like this:
$html = '<td style="font-size:24px;" bgcolor="#F0F0F0" width="60%"> <strong>ID:Full Name:email#email.com:Mobile:Country</strong></td>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
$text = $xpath->query('//td/strong')->item(0)->nodeValue;
echo $text; // ID:Full Name:email#email.com:Mobile:Country
This question already has answers here:
XPath get attribute value in PHP [duplicate]
(3 answers)
Closed 8 years ago.
HTML
<input type='text' name='title[]' value='Some word and another'>
PHP
$title = $xpath->query('//input')->item(0);
echo $title = $title->getAttribute('value')
RESULT
Some
I NEED TO GET
Some word and another
PROBLEM
For some reason everything after first space is stripped out..
Be sure to load it as HTML, so that it allows you to use single quotes:
$dom = new DOMDocument;
$dom->loadHTML("<input type='text' name='title[]' value='Some word and another'>");
$xpath = new DOMXpath($dom);
echo $xpath->query('//input')->item(0)->getAttribute('value');
See it here in action: http://codepad.viper-7.com/pcs3or
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
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);