douquan9826
2011-08-19 09:36
浏览 75
已采纳

在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?

图片转代码服务由CSDN问答提供 功能建议

我有一些必须处理的UTF8文本+图像数据。

我的整个代码都在一个文件中; 这是完整的代码:

 &lt;?php 
echo“&lt; html xmlns = \”http://www.w3.org/1999/xhtml \“&gt  ; 
&lt; head&gt;&lt; meta http-equiv ='Content-Type'content ='text / html; charset = utf-8'/&gt;&lt; / head&gt;&lt; body&gt;“; 
 
 $  article_header =“აბგდევზთ&lt; img src ='some_url / img / 15.jpg'alt =''&gt;აბგდევზთ”; 
echo“1”。$ article_header。“&lt; br&gt;”; 
 $ doc = new DOMDocument(  ); 
 $ doc-&gt; loadHTML($ article_header); 
 $ imgs = $ doc-&gt; getElementsByTagName('img'); 
foreach($ imgs as $ img){
 if(!$ img-)  &gt; getAttribute('class')){
 $ src = $ img-&gt; getAttribute('src'); 
 $ newSRC = str_replace('/ img /','/ mini /',$ src);  
 $ img-&gt; setAttribute('src',$ newSRC); 
 $ img-&gt; removeAttribute('width');  
 $ img-&gt; removeAttribute('height'); 
 $ article_header = $ doc-&gt; saveHTML(); 
} 
} 
echo“2”。$ article_header。“&lt; br&gt;”;  
echo“&lt; / body&gt;&lt; / html&gt;”; 
?&gt; 
   
 
 

如您所见,我回收数据2次。

  • 第一次,它会同时显示文字和图像。

  • 第二次,它 按预期显示修改后的图像。 但是文本被损坏了,如下所示:áƒáƒ'áƒ'დევáƒ-რ-

    有没有办法解决这个问题?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duansha7453 2011-08-19 10:54
    已采纳

    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!!!!

    点赞 打赏 评论

相关推荐 更多相似问题