“\ u00e1n”到“á”在PHP [英] "\u00e1n" to "á" in PHP

查看:407
本文介绍了“\ u00e1n”到“á”在PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个带有字符串的外部文件,我不能控制该文件,但我收到特殊字符作为这个(我认为它是unicode?)序列\\\án。



谢谢!



p> EDIT:



我正在呼叫一个网址,其中列出了人名:



Tom \\\ánsson\\\

Eriksen \\\

Gilverto \\\



我正在读姓名

解决方案

EDIT



现在,我已经找出了正确的,工作的答案基于我发现的文章。这是它:

  function unicode2utf8($ str){
$ a = json_decode(str_replace(\\\
,< br>,['。$ str。']'));
return str_replace(< br>,\\\
,$ a [0]);
}

PHP:

  echo unicode2utf8(Tom \\\ánsson\\\
Eriksen \ Gilverto \\\
);

输出:

 code>Tománsson
Eriksen
Gilverto






ORIGINAL



我在这里找到一篇关于同样问题的文章( http://www.welefen.com/php-unicode-to-utf8.html



解决方案是以下函数

  function unicode2utf8($ str){
if(!$ str)return $ str;
$ decode = json_decode($ str);
if($ decode)return $ decode;
$ str ='['。$ str。']';
$ decode = json_decode($ str);
if(count($ decode)== 1){
return $ decode [0];
}
return $ str;
}

我试过了

  echo unicode2utf8(\\\á); 

输出:

 code>á


I am receiving an external file with strings, I have no control over that file but I receive special characters as this (i think it's unicode?) sequence \u00e1n.

Is there anyway to convert this kind of char sequences to their "readable" counterparts?

Thanks!

EDIT:

I am calling a url that gives me a list with names of persons:

Tom\u00e1nsson\n Eriksen\n Gilverto\n

I am reading the names and showing them in my site.

解决方案

EDIT

Now, I've figured out the correct, working answer based on the article I found. Here is it:

function unicode2utf8($str) {
    $a=json_decode(str_replace("\n","<br>",'["'.$str.'"]'));
    return str_replace("<br>","\n",$a[0]);
}

PHP:

echo unicode2utf8("Tom\u00e1nsson\n Eriksen\n Gilverto\n");

Output:

Tománsson
 Eriksen
 Gilverto


ORIGINAL

I've found an article about the same problem here ( http://www.welefen.com/php-unicode-to-utf8.html )

The solution is the following function

function unicode2utf8($str){
    if(!$str) return $str;
    $decode = json_decode($str);
    if($decode) return $decode;
    $str = '["' . $str . '"]';
    $decode = json_decode($str);
    if(count($decode) == 1){
            return $decode[0];
    }
    return $str;
}

I've tried it with

echo unicode2utf8("\u00e1");

Output:

á

这篇关于“\ u00e1n”到“á”在PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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