2

我正在尝试从另一个网站上的 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?

请注意,我期待的连字符不是正常的减号,而是一些大屁股超级破折号。好吧,无论如何要长几个像素。:) 不知道如何描述它,因为我的键盘无法产生那个字符......

4

4 回答 4

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 );

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

于 2009-10-14T15:58:55.007 回答
4

我怀疑你的意思是 Em Dash (-)。ISO-8859-1 不包含此字符,因此您不会很幸运地将其转换为该编码。

您可以使用htmlentities(),但我建议将 ISO-8859-1 转换为 UTF-8 进行发布。

于 2009-10-14T15:33:38.910 回答
3
于 2009-10-14T16:06:00.490 回答
1

它可能是一个破折号(U + 2014),您要做的不是转换编码,因为连字符是一个不同的字符。换句话说,您想搜索这些字符并手动替换它们。

更好的是,只需将网站切换到 UTF-8。它在很大程度上与 Latin-1 一致,更适合 2009 年的网站。

于 2009-10-14T15:43:57.477 回答