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
Related
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
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 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.
This question already has answers here:
SimpleXML: Selecting Elements Which Have A Certain Attribute Value
(2 answers)
Closed 8 years ago.
I am new to processing and reading XML strings in PHP
I have XML like this
<sports-metadata>
<sports-content-codes>
<sports-content-code code-type="sport" code-key="15027000" code-name="Golf"/>
<sports-content-code code-type="league" code-key="l.pga.com" code-name="Professional Golf Association"/>
<sports-content-code code-type="season-type" code-key="regular"/>
<sports-content-code code-type="season" code-key="2015"/>
<sports-content-code code-type="priority" code-key="normal"/>
</sports-content-codes>
</sports-metadata>
I have read in the XML via a $xml=simplexml_load_file()
I can get to this XML section via $xml->{'sports-content-codes'}->{'sports-content-code'}
In sports-content-code
I want to access/retrieve the code-key value where code-type="season"
How can I do this in PHP?
Thank you all.
-- Ed
Usually you use ->attributes() method to get those attributes:
foreach($xml->{'sports-content-codes'}->{'sports-content-code'} as $content_code) {
$attr = $content_code->attributes();
$code_type = (string) $attr->{'code-type'};
echo $code_type;
}
Sample Output
Use Xpath ...
... with SimpleXml:
$element = simplexml_load_string($xml);
$array = $element->xpath(
'//sports-content-code[#code-type="season"]'
);
var_dump(
(string)$array[0]['code-key']
);
... or DOM:
$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);
var_dump(
$xpath->evaluate(
'string(//sports-content-code[#code-type="season"]/#code-key)'
)
);
Output (both):
string(4) "2015"
Xpath is a expression language for DOM (think SQL for DBMS). SimpleXMLElement::xpath() supports some of it (only expressions that return element or attribute nodes). The result will always be an array of SimpleXMLElement objects. DOMXpath::evaluate() supports full Xpath 1.0. The result is a DOMNodelist or a scalar value, depending on the expression.
The expression:
Select the "sports-content-code" element nodes
//sports-content-code
with a "code-type" attribute node is season
//sports-content-code[#code-type="season"]
get the "code-key" attribute nodes
//sports-content-code[#code-type="season"]/#code-key
cast the node list to a string returning the text content of the first node
string(//sports-content-code[#code-type="season"]/#code-key)
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);