【问题标题】:PHP: Dealing special characters with iconvPHP:用 iconv 处理特殊字符
【发布时间】:2011-06-15 05:51:48
【问题描述】:

我仍然不明白iconv 的工作原理。

例如,

$string = "Löic & René";
$output = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $string); 

我明白了,

注意:iconv() [function.iconv]: 在输入中检测到非法字符 字符串...

$string = "Löic";$string = "René";

我明白了,

注意:iconv() [function.iconv]: 在输入字符串中检测到不完整的多字节字符。

$string = "&"; 我一无所获

我需要将两组不同的输出存储在我的数据库表内的两个不同列中,

  1. 我需要将Löic & René 转换为Loic & Rene 以实现干净的网址。

  2. 我需要保持它们原样 - Löic & RenéLöic & René 然后仅在我的 html 页面上显示它们时将它们转换为 htmlentities($string, ENT_QUOTES);

我尝试了下面php.net 中的一些建议,但还是不行,

我遇到过需要音译一些字符,但其他字符被忽略的情况(对于像 ayn 或 hamza 这样的奇怪变音符号)。添加 //TRANSLIT//IGNORE 似乎对我有用。它会音译所有可以音译的内容,但随后会抛出无法音译的内容。

所以:

$string = "ʿABBĀSĀBĀD";

echo iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $string);
// output: [nothing, and you get a notice]

echo iconv('UTF-8', 'ISO-8859-1//IGNORE', $string);
// output: ABBSBD

echo iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $string);
// output: ABBASABAD
// Yay! That's what I wanted!

还有一个,

Andries Seutens 07-Nov-2009 07:38
When doing transliteration, you have to make sure that your LC_COLLATE is properly set, otherwise the default POSIX will be used.

To transform "rené" into "rene" we could use the following code snippet:
setlocale(LC_CTYPE, 'nl_BE.utf8');

$string = 'rené';
$string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);

echo $string; // outputs rene

我怎样才能真正解决它们?

谢谢。

编辑:

这是我测试代码的源文件,

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" class="no-js">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<?php
$string = "Löic & René";
$output = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $string); 
?>
</html>

【问题讨论】:

  • 顺便说一句:你知道öé 在ISO-8859-1 中吗?除了不正确的输入字符集之外,您可能希望将输出字符集更改为 ASCII//TRANSLIT
  • 我对这些字符集感到很困惑......
  • 谢谢!我不得不将一些韩文字符解码为 UTF-8,这真是令人头疼 - 最终,唯一有帮助的是:$converted = iconv('EUC-KR', 'UTF-8//TRANSLIT', $data);

标签: php special-characters iconv


【解决方案1】:
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', utf8_encode($s));

【讨论】:

  • +1, utf8_encode() 对我来说很好用,谢谢!
  • 请注意,utf8_encode() 只会将 ISO-8859-1 转换为 UTF-8
  • 很棒的sn-p。我在文件名转换中遇到了通知,这个解决方案很好地解决了我的问题。
【解决方案2】:
猜你喜欢
  • 2019-10-13
  • 2018-09-24
  • 1970-01-01
  • 1970-01-01
  • 2018-11-06
  • 1970-01-01
  • 1970-01-01
  • 2014-09-13
  • 2011-11-16
相关资源
最近更新 更多