How to extract Chinese characters and figure out the position of the character

Source: Internet
Author: User
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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.