UTF8到php中的等效数字 [英] UTF8 to equivalent number in php
问题描述
基本上我正在读取一个UTF-8 .txt文件在PHP中,然后保存数组中的每一行,所以我可以搞砸它。
如果有人可以帮助我,这将是非常感谢,因为我是不熟悉UTF8。
编辑:
这是我到目前为止这样:
echovar TextCharacters = new Array(); \\\
;
$ LineArray = array();
$ file_handle = fopen(lesson1.txt,r);
while(!feof($ file_handle))
{
$ line_of_text = fgets($ file_handle);
array_push($ LineArray,$ line_of_text);
}
fclose($ file_handle);
foreach($ LineArray as $ s)
{
for($ i = 0; $ i< mb_strlen($ s,utf-8); $ i ++ )
{
$ char = mb_substr($ s,$ i,1,utf-8);
echoalert(go(。bin2hex(iconv('UTF-8','UCS-2',$ char)))));;
}
}
您正在寻找的是Unicode代码点,即Unicode字符表中已知字符的数字标识符。 最便宜的方法是通过UCS-2字符编码,将1:1从字节映射到Unicode代码点:
echo bin2hex(iconv('UTF-8','UCS-2','あ'));
// 3042
注意事项:返回的代码始终为4个十六进制数字可能或可能不喜欢),UCS-2不支持高于BMP的字符,即高于代码点FFFF。
I've been searching my !!! off trying to find a PHP function to convert UTF8 to the equivalent number. I'm not entirely sure what to call the number (I heard its called an ordinate?) but heres an example: http://jrgraphix.net/r/Unicode/3040-309F
Basically I'm trying to read a UTF-8 .txt file in PHP and then save every line in an array, so I can mess around with it.
If anyone can assist me with this it would be highly appreciated, as I am not that familiar with UTF8 yet.
Edit: This is what I've got so far:
echo "var TextCharacters = new Array();\n";
$LineArray = array();
$file_handle = fopen("lesson1.txt", "r");
while (!feof($file_handle))
{
$line_of_text = fgets($file_handle);
array_push($LineArray, $line_of_text);
}
fclose($file_handle);
foreach($LineArray as $s)
{
for($i = 0; $i < mb_strlen($s,"utf-8"); $i++)
{
$char = mb_substr($s, $i, 1, "utf-8");
echo "alert(go(" . bin2hex(iconv('UTF-8', 'UCS-2', $char)) . "));";
}
}
What you're looking for is the Unicode code point, i.e. the numeric identifier by which the character is known in the Unicode character table. The "cheapest" way to do this is through the UCS-2 character encoding, which maps 1:1 from bytes unto the Unicode code points:
echo bin2hex(iconv('UTF-8', 'UCS-2', 'あ'));
// 3042
Caveats: the returned code is always 4 hexadecimal digits long (which you may or may not like) and UCS-2 does not support characters higher than the BMP, i.e. higher than code point FFFF.
这篇关于UTF8到php中的等效数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!