1

I have this simple code:

$input = '<p>ěščřžýáíé</p><p><img alt="" src="http://www.test.com/img.jpg" style="width: 100px; height: 100px;"></p>';
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->encoding = 'UTF-8';
$dom->loadHTML($input, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$imgs = $dom->getElementsByTagName('img');
foreach($imgs as $img){
    $src = $img->getAttribute('src');
    $style = $img->getAttribute('style');
    $newSrc = 'http://www.test.com/img001.jpg';
    $img->setAttribute( 'src' , $newSrc );
}
$content = $dom->saveHTML();

Problem is that output is encoded. I expect same characters as are on input. I tried decoding without success. Something wrong with using DOM object?

<p>&Auml;&#155;&Aring;&iexcl;&Auml;&#141;&Aring;&#153;&Aring;&frac34;&Atilde;&frac12;&Atilde;&iexcl;&Atilde;&shy;&Atilde;&copy;<p><img alt="" src="http://www.test.com/img001.jpg" style="width: 100px; height: 100px;"></p></p>
1
  • The output is encoded in what way? url encoded, character entities? Commented Nov 22, 2018 at 16:08

1 Answer 1

2

saveHTML() has a few 'features' which I don't understand, but when saving with a particular document node it will work if you then utf8_decode() the result...

$content = utf8_decode($dom->saveHTML($dom->documentElement));

gives...

<p>ěščřžýáíé<p><img alt="" src="http://www.test.com/img001.jpg" style="width: 100px; height: 100px;"></p></p>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.