How do I extract Chinese characters and figure out the position of the character?
String 1: One? Two? Question mark in Chinese
String 2:? A. Two//question mark in English
String 3: One or two//with spaces in the middle
How does PHP match the Chinese text in the above string? and find out the position of the character
It's been a while, and the following is my code, everybody, no jokes.
$k 1 = "";
$k 2 = "";
$k 3 = "";
$k 4 = "";
if (Ord (substr ($this->keywords,1,1)) >127)
{
$k 1 = substr ($this->keywords,0,2);
}else {$k 1=false;}
if (Ord (substr ($this->keywords,2,1)) >127)
{
$k 2 = substr ($this->keywords,2,2);
}else {$k 2=false;}
if (Ord (substr ($this->keywords,3,1)) >127)
{
$k 3 = substr ($this->keywords,3,2);
}else {$k 3=false;}
if (Ord (substr ($this->keywords,4,1)) >127)
{
$k 4 = substr ($this->keywords,4,2);
}else {$k 4=false;}
------Solution--------------------
It depends on whether it is UTF8 or gb2312.
I think you're UTF8 's test. UTF8 Chinese is 3 bytes, head byte >127.
After Str_split, more than 127 is detected, and the latter 2 bytes are also acquired.
------Solution--------------------
$k 1 = mb_substr ($this->keywords, 0, 1, "UTF-8");
------Solution--------------------
$str = ' one? Two? ';
$arr = Preg_split ('/\?
------Solution--------------------
?
------Solution--------------------
/', $STR,-1, preg_split_offset_capture);
Print_r ($arr);
Get a two-dimensional array, $arr [$i][0] matches the Met character, $arr [$i][1] Match location
This position is strlen, if the Chinese count a character, only need to intercept the previous paragraph of the character, with Mb_strlen can