【问题标题】:HTML DOM PARSER UTF-8HTML DOM 解析器 UTF-8
【发布时间】:2013-04-01 16:47:30
【问题描述】:

我有一个网站,但没有使用 UTF-8 编码。我在另一个 php 中包含 php 文件。当我将编码更改为 UTF-8 时,所有字符都损坏了。所以我不能使用 header(..utf8 bla bla tag.

include_once 'includes/simple_html_dom.php';

$ozet = file_get_contents($url);
$html = str_get_html($ozet);
$trozet = $html->find('div[class="TEST"]',0)->plaintext;
$icerik = "";
$yazi = "<span>$trozet</span>";
$uzunluk = strlen($yazi);
$sinir = 155;
if ($uzunluk > $sinir) {
$icerik = substr($yazi,0,$sinir) . "...";
}

$content.= '<i><span>'.$icerik.'</span></i>';

return $content;

但它得到的 html 是这样的:

Pittsburgh kentinde sakin ve güneşli bir sabah, mesai saatinden hemen önce insanlar işlerine doğru koşturmakta, günlük telaşlarını yaşama...

应该是:

Pittsburgh kentinde sakin ve güneşli bir sabah, mesai saatinden hemen önce insanlar işlerine doğru koşturmakta, günlük telaşlarını...

我怎样才能做到这一点?

【问题讨论】:

    标签: php html utf-8 domparser


    【解决方案1】:

    要 Substr utf-8 字符串,您可以使用如下函数:

    function substrutf8($str,$from,$len){
    return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'. $from .'}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'. $len .'}).*#s','$1', $str);}
    

    【讨论】:

      【解决方案2】:

      如果不能使用 UTF-8,则必须将其转换为其他编码:

      $yazi = mb_convert_encoding("<span>$trozet</span>", "Windows-1250", "UTF-8");
      

      请注意,并非每个网站都使用 UTF-8,而且 Windows-1250 仅支持一小部分 Unicode 字符。

      【讨论】:

        猜你喜欢
        • 2020-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-07
        • 1970-01-01
        • 1970-01-01
        • 2015-08-25
        • 2015-07-22
        相关资源
        最近更新 更多