utf-8到iso-8859-1编码问题

Mat*_*hew 1 php rss encoding utf-8 iso-8859-1

我正在尝试在另一个网站上预览来自rss feed的最新帖子.Feed是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?
Run Code Online (Sandbox Code Playgroud)

请注意,我期待的连字符不是正常的减号,而是一些大笨钟.好吧,反正还要几个像素.:)不确定如何描述它,因为我的键盘无法生成该字符...

Pet*_*ley 5

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 );
Run Code Online (Sandbox Code Playgroud)

或者,正如其他人所说,只是将超出范围的字符转换为html实体.


Gum*_*mbo 5

我想有以下几点:

\n\n
    \n
  • 您的文件实际上是用 UTF-8 编码的
  • \n
  • 您的编辑器使用 Windows-1252 解释该文件
  • \n
\n\n

原因是您的EM DASH字符 (U+2014) 由 表示\xc3\xa2\xe2\x82\xac\xe2\x80\x9c\xe2\x80\x99 正是用Windows-1252(0xE2= \xc3\xa2、 0x80= \xe2\x82\xac、 0x94= )解释该字符 (0xE28094) 的 UTF-8 代码字时得到的结果\xe2\x80\x9d。因此,您首先需要修复编辑器编码。

\n\n

输出中的原因?是 ISO 8859-1 不包含EM DASH字符。

\n