I still don't understand how iconv
works.
我还是不明白iconv是怎么工作的。
For instance,
例如,
$string = "Löic & René";
$output = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $string);
I get,
我得到了,
Notice: iconv() [function.iconv]: Detected an illegal character in input string in...
注意:iconv()函数。:在…的输入字符串中检测到一个非法字符。
$string = "Löic";
or $string = "René";
$ string =“卢”;或字符串美元=“刘若英”;
I get,
我得到了,
Notice: iconv() [function.iconv]:
Detected an incomplete multibyte character in input string in.
注意:iconv()函数。iconv:在输入字符串中检测到一个不完整的多字节字符。
I get nothing with $string = "&";
$string = "&"我什么都得不到;
There are two sets of different outputs I need store them in the two different columns inside the table of my database,
我需要将两组不同的输出存储在数据库表中的两个不同列中,
-
I need to convert
Löic & René
toLoic & Rene
for clean url purposes.我需要将Loic和Rene转换为Loic和Rene,以实现清晰的url。
-
I need to keep them as they are -
Löic & René
asLöic & René
then only convert them withhtmlentities($string, ENT_QUOTES);
when displaying them on my html page.我需要保持它们的原样——Loic和Rene作为Loic和Rene,然后只使用htmlentities($string, ENT_QUOTES)转换它们;当在我的html页面上显示它们时。
I tried with some of the suggestions in php.net below, but still don't work,
我尝试了以下php。net中的一些建议,但仍然无效,
I had a situation where I needed some characters transliterated, but the others ignored (for weird diacritics like ayn or hamza). Adding //TRANSLIT//IGNORE seemed to do the trick for me. It transliterates everything that is able to be transliterated, but then throws out stuff that can't be.
我曾经遇到过这样的情况:我需要一些音译字符,但其他字符却被忽略了(比如ayn或hamza)。添加//TRANSLIT//IGNORE对我来说似乎很管用。它把所有能被音译的东西都音译了出来,然后又把那些不能被音译的东西扔掉。
So:
所以:
$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!
and another,
另一个,
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
How can I actually work them out?
我怎么才能算出来呢?
Thanks.
谢谢。
EDIT:
编辑:
This is the source file I test the code,
这是我测试代码的源文件,
<!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>
2 个解决方案
#1
13
And did you save your source file in UTF-8 encoding? If not (and I guess you didn't since that will produce the "incomplete multibyte character" error), then try that first.
您是否将源文件保存为UTF-8编码?如果没有(我猜您没有,因为这会产生“不完整的多字节字符”错误),那么请先尝试一下。
#2
19
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', utf8_encode($s));
#1
13
And did you save your source file in UTF-8 encoding? If not (and I guess you didn't since that will produce the "incomplete multibyte character" error), then try that first.
您是否将源文件保存为UTF-8编码?如果没有(我猜您没有,因为这会产生“不完整的多字节字符”错误),那么请先尝试一下。
#2
19
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', utf8_encode($s));