【发布时间】: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 = "&"; 我一无所获
我需要将两组不同的输出存储在我的数据库表内的两个不同列中,
我需要将
Löic & René转换为Loic & Rene以实现干净的网址。我需要保持它们原样 -
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