PHP:使用iconv处理特殊字符 [英] PHP: Dealing special characters with iconv

查看:560
本文介绍了PHP:使用iconv处理特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然不了解iconv的工作方式.

I still don't understand how iconv works.

例如,

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

我明白了

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

Notice: iconv() [function.iconv]: Detected an illegal character in input string in...

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

我明白了

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

Notice: iconv() [function.iconv]: Detected an incomplete multibyte character in input string in.

$string = "&";

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

There are two sets of different outputs I need store them in the two different columns inside the table of my database,

  1. 我需要将Löic & René转换为Loic & Rene以用于干净的url.

  1. I need to convert Löic & René to Loic & Rene for clean url purposes.

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

I need to keep them as they are - Löic & René as Löic & René then only convert them with htmlentities($string, ENT_QUOTES); when displaying them on my html page.

我尝试了以下 php.net 中的一些建议,但仍然无法正常工作,

I tried with some of the suggestions in php.net below, but still don't work,

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

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.

所以:

$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

我该如何实际解决呢?

谢谢.

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

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>

推荐答案

您是否以UTF-8编码保存了源文件?如果不是这样(我想您可能没有,因为那样会产生不完整的多字节字符"错误),那么请首先尝试.

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.

这篇关于PHP:使用iconv处理特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆