32

我仍然不明白如何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é因为只有在我的 html 页面上显示它们Löic & René时才转换它们。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>
4

2 回答 2

27
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', utf8_encode($s));
于 2012-02-08T20:25:55.050 回答
14

您是否将源文件保存为 UTF-8 编码?如果不是(我猜你没有,因为那会产生“不完整的多字节字符”错误),然后先尝试。

于 2011-01-25T14:41:00.257 回答