在PHP中使用DOM对象时出现UTF8问题

I have some UTF8 text+image data which must be processed.

My whole code is in one file; here is the complete code:

<?php
echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' /></head><body>";

$article_header="აბგდევზთ<img src='some_url/img/15.jpg' alt=''>აბგდევზთ";
echo "1".$article_header."<br>";
$doc = new DOMDocument();
$doc->loadHTML($article_header);
$imgs = $doc->getElementsByTagName('img');
foreach ($imgs as $img) {
 if(!$img->getAttribute('class')){
$src = $img->getAttribute('src');
$newSRC = str_replace('/img/', '/mini/', $src);
$img->setAttribute('src', $newSRC);
$img->removeAttribute('width'); 
$img->removeAttribute('height');
$article_header = $doc->saveHTML();
            }
                        }
echo "2".$article_header."<br>";
echo "</body></html>";
?>

As you see I echo data 2 times.

  • The first time, it brings both text and image, as expected.

  • The second time, it brings the modified image as expected. But the text becomes damaged, like this: áƒáƒ‘გდევზთ

Is there any way to fix this problem?

Guys I've found the solution!!!!!!!!!! Huraaa !!!! :)))) For those who will face this problem in future here is the code

$article_header = mb_convert_encoding($article_header, 'HTML-ENTITIES', "UTF-8");

This must be done before loadHTML and everything works fine!!!!