i have some problems to retreive the HTML of a specefic div containing the class name “tabEmbed”
The code works but for some reason, it only returns the txt … no HTML. All the text formating is striped out … normaly there should be embed code as well. What did i miss?
$dom = new DOMDocument;
@$dom->loadHTML($Video_Source_HTML);
$Div_Data = $dom->getElementsByTagName('div');
foreach($Div_Data as $Div) {
if ($Div->getAttribute('id') === 'tabEmbed') {
$Embed_HTML = $Div->nodeValue;
if(preg_match('/<input type="text"(.*?)">/is', $Embed_HTML, $Embed)) {
$Embed_Code = $Embed[1];
}
}
}
Website Video Embed Code Source
<div id="tabEmbed" class="tab">
<h3 class="blackTitle">Embed this video to your site with this code:</h3>
<input type="text" name="media_embed_code" id="mediaEmbedCodeInput" size="110" onclick="this.focus();this.select();" value="<iframe src="" frameborder=0 width=510 height=400 scrolling=no></iframe>">
</div>
$Embed_HTML Source:
Embed this video to your site with this code:
First off, you can greatly simplify your code by using
DOMDocument::getElementById()in stead of usingDOMDocument::getElementsByTagName().Then, if you’re using a version of PHP equal to, or higher than 5.3.6, you can use
DOMDocument::saveHTML()to output the “outerHTML” of the node, like so:And, as per GordonM’s helpful comment, you could use
DOMDocument::saveXML()in PHP versions before 5.3.6, to get acceptable results (depending on the actual content of the original HTML), like so: