我有一些必须处理的UTF8文本+图像数据。在PHP中使用DOM对象时出现UTF8问题
我的整个代码都在一个文件中;这里是完整的代码:
<?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>";
?>
正如你所看到的我回显数据2次。
第一次,它带来了文本和图像,如预期。
第二次,它带来了预期的修改后的图像。但文字被损坏,像这样:
áƒáƒ‘გდევზთ
有什么办法解决这个问题吗?
我的编辑器也无法识别这些字符。你确定这些是UTF-8吗? –
那些是格鲁吉亚的信件。如果你想要的话,你可以试试俄罗斯信件。结果是一样的。或者甚至更好的在这里写下你认为可以的utf8字母,我会给他们一个尝试。 – David
我尝试了繁体中文(检测 - 测试)这两个工作。我也测试过希腊语(δοκιμή - test),也适用。 –