Reputation: 13333
In php I want to replace image src from html with my custom url. For that I have made a function like this
function replace_img_src($img_tag) {
$doc = new DOMDocument();
$doc->loadHTML($img_tag);
$tags = $doc->getElementsByTagName('img');
foreach ($tags as $tag) {
$old_src = $tag->getAttribute('src');
$new_url = rawurlencode($old_src);
$new_src_url = get_template_directory_uri() . '/img.php?img='.$new_url;
$tag->setAttribute('src', $new_src_url);
}
return $doc->saveHTML();
}
its working fine but it returns total html like this
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p class='\"\"'>hello world</p><div class='\"medium-insert-images\"'><img src="http://test.com/dev/projects/newatarlife/wp-content/themes/atarlife/img.php?img=%5C%22https%3A%2F%2Fpixabay.com%2Fget%2Feb32b40620f41c2ad65a5854e44a4092e27fe5c818b517469df8c97ba5ee_640.jpg%5C%22"></div><div class='\"medium-insert-buttons\"' contenteditable='\"false\"' style='\"left:' top: display: none>
<a class='\"medium-insert-buttons-show\"'> </a>
<ul class='\"medium-insert-buttons-addons\"' style='\"display:' none><li><a data-addon='\"images\"' data-action='\"add\"' class='\"medium-insert-action\"'><span class='\"fa' fa-camera></span></a></li>
<li><a data-addon='\"embeds\"' data-action='\"add\"' class='\"medium-insert-action\"'><span class='\"fa' fa-youtube-play></span></a></li>
</ul></div></body></html>
I only want the content inside body. Not the whole html.
Upvotes: 1
Views: 118
Reputation: 1078
here is an option in loadHTML() method to get rid of the HTML wrapper :
$doc->loadHTML($img_tag, , LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
https://php.net/manual/domdocument.loadhtml.php
https://php.net/manual/libxml.constants.php
Upvotes: 2
Reputation: 89547
If you are working with html parts (ie: not a whole page that includes a DTD and html, body tags), there's two possible scenarios: there's a root element that encloses all the other nodes, or there isn't a root element.
To deal with these two scenarios at the same time, the workaround consists to artificially add a root element (without a root element, LIBXML isn't able to correctly build the DOM tree):
$doc = new DOMDocument();
$doc->loadHTML('<div>' . $img_tag . '</div>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
LIBXML_HTML_NOIMPLIED
prevents to automatically add the html and body elements.
LIBXML_HTML_NODEFDTD
prevents to add the DTD. (this one isn't really needed)
Note: sometimes the constants LIBXML_HTML_NOIMPLIED
and LIBXML_HTML_NODEFDTD
are not defined. In this case you can define them yourself before or replace them with their values that are 8192 and 8. Or you can directly put the two values sum: 8200.
Then to return the html you want (without the fake root element), you need to concatenate all root element child nodes:
$result = '';
foreach($doc->documentElement->childNodes as $childNode)
$result .= $doc->saveHTML($childNode);
return $result;
$doc->documentElement
is a convenient way to target the root element.
Other possibility: don't use the LIBXML_HTML_NOIMPLIED
option, don't add the fake root element and concatenate body child nodes:
$doc->loadHTML($img_tag);
...
$root = $doc->getElementsByTagName('body')->item(0);
$result = '';
foreach ($root->childNodes as $childNode)
$result .= $doc->saveHTML($childNode);
return $result;
Obviously you can't use this workaround with a document that already has an html and a body tag.
Upvotes: 0