问题描述
我有一个国家/地区数组,其中有一个拉丁字符Å:
$ country [af] =阿富汗;
$ country [ax] =ÅlandIslands;
$ country [al] =阿尔巴尼亚;
在循环访问此数组并执行国家/地区名称的第一个字符比较时,拉丁字符。
foreach($ country as $ cc => $ name)
{
if($ name [0] ==Å)
{
echomatched;
}
else
{
echo $ name [0];
}
}
我得到的结果是:A A
为什么拉丁字符成为 ,如何进行正确的比较并输出拉丁字符?
添加注意:http头和html文档已经被指定为UTF-8格式。
添加注释2:如果我只是回显 $ name
而不是 $ name [0]
,我可以在Åland群岛获得Å。使用 substr($ name,0,1)
与 $ name [0]
将您的脚本更改为此。 unicode编码字不能与正常的字符串函数爆炸。您必须使用多字节函数。
foreach($ country as $ cc => $ name)
{
if(mb_substr($ name,0,1,UTF-8)==Å)
{
echomatched;
}
else
{
echo mb_substr($ name,0,1,UTF-8);
}
}
I have an array of countries with one having a Latin character "Å":
$country["af"] = "Afghanistan";
$country["ax"] = "Åland Islands";
$country["al"] = "Albania";
While looping through this array and performing a comparison of the first character of the country name, I cannot match the Latin character.
foreach($country as $cc => $name)
{
if($name[0] == "Å")
{
echo "matched";
}
else
{
echo $name[0];
}
}
The result I got is: A�A
Why does the Latin character Å became � and how do I perform a proper comparison and output the Latin character Å?
Add Note: The http header and the html document have already been specified as UTF-8 format.
Add Note2: If I just echo $name
instead of $name[0]
, I am able to get the Å in Åland Islands. Using substr($name, 0, 1)
has the same effect as $name[0]
, which gives me �.
Change your script to this. The unicode encoding words cannot explode with normal string functions. You have to use multibyte functions.
foreach($country as $cc => $name)
{
if(mb_substr($name,0,1,"UTF-8") == "Å")
{
echo "matched";
}
else
{
echo mb_substr($name,0,1,"UTF-8");
}
}
这篇关于如何比较和输出拉丁字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!