我正在尝试从另一个网站上的 RSS 提要预览最新的帖子。提要采用 UTF-8 编码,而网站采用 ISO-8859-1 编码。显示标题时,我正在使用;
$post_title = 'Blogging – does it pay the bills?';
echo mb_convert_encoding($post_title, 'iso-8859-1','utf-8');
// returns: Blogging ? does it pay the bills?
// expected: Blogging - does it pay the bills?
请注意,我期待的连字符不是普通的减号,而是一些大屁股的 super 破折号。好吧,无论如何要长几个像素。 :) 不确定如何描述它,因为我的键盘无法产生该字符...
最佳答案
mb_convert_encoding
仅转换内部编码 - 它实际上不会将字符的字节序列从一种字符集更改为另一种字符集。为此你需要 iconv .
mb_internal_encoding( 'UTF-8' );
ini_set( 'default_charset', 'ISO-8859-1' );
$post_title = 'Blogging — does it pay the bills?'; // I used the actual m-dash here to best mimic your scenario
echo iconv( 'UTF-8', 'ISO-8859-1//TRANSLIT', $post_title );
或者,正如其他人所说,只需将超出范围的字符转换为 html 实体。
关于php - utf-8转iso-8859-1编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1567100/