PHP和字符编码问题与Â字符 [英] PHP and character encoding problem with  character
问题描述
我有一个问题,PHP(5.2)在字符串中找不到字符Â,虽然很明显。
我意识到底层问题与字符编码有关,但不幸的是我无法控制源内容。我收到它作为UTF-8,这些字符已经在字符串。
我只想从字符串中删除它。 strpos(),str_replace(),preg_replace(),trim()等无法正确识别它。
我的字符串是这样的:
ÂÂ世界各地的情侣
如果我这样做:
$ string = str_replace('Â','',$ string);
我得到这个:
 p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p >我甚至在str_replace之前尝试过utf8_encode()和utf8_decode(),没有运气。
有什么解决方案?我一直在扔我可以找到的一切...
解决方案我使用这个:
function replaceSpecial($ str){
$ chunked = str_split($ str,1);
$ str =;
foreach($ chunked as $ chunk){
$ num = ord($ chunk);
//删除非ascii&非html字符
if($ num> = 32&& $ num <= 123){
$ str。= $ chunk;
}
}
return $ str;
}
I'm having a problem where PHP (5.2) cannot find the character 'Â' in a string, though it is clearly there.
I realize the underlying problem has to do with character encoding, but unfortunately I have no control over the source content. I receive it as UTF-8, with those characters already in the string.
I would simply like to remove it from the string. strpos(), str_replace(), preg_replace(), trim(), etc. Cannot correctly identify it.
My string is this:
"Â Â Â A lot of couples throughout the World "
If I do this:
$string = str_replace('Â','',$string);
I get this:
"� � � A lot of couples throughout the World"
I even tried utf8_encode() and utf8_decode() before the str_replace, with no luck.
What's the solution? I've been throwing everything I can find at it...
解决方案 I use this:
function replaceSpecial($str){
$chunked = str_split($str,1);
$str = "";
foreach($chunked as $chunk){
$num = ord($chunk);
// Remove non-ascii & non html characters
if ($num >= 32 && $num <= 123){
$str.=$chunk;
}
}
return $str;
}
这篇关于PHP和字符编码问题与Â字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!