The Ord function of PHP?? Resolving Chinese character truncation problems

Source: Internet
Author: User
The function is defined like this:

int ord (string$string)

Returns the ASCII code value of the first character of a string.

This function is a complementary function of Chr ().

Try it:

Echo Ord (' I ');
Here can only return 230, I was saved by U8 file and output, it got only 230, and 230 converted to Hex is E6, actually utf-8 My code is e68891, it only got the first byte
echo chr (0xe6) Chr (0x88). chr (0x91);

This example can output the character "I" in the case of Utf-8.

If you want to understand the character encoding problem can dot here character code

If you want to see a Chinese character gbk,utf-8,unicode various coding methods recommended everyone with notepad++ under the Hex-editor click here to download:

Http://pan.baidu.com/s/1hquyJwo

Long like this.


Increase the force lattice:

As a procedural ape, in addition to a synonym for bitterness, but also a mysterious symbol, occasionally installed XX or good. Since it comes to coding,

So let's talk about your word, in the world of utf-8 coding, not everyone can find their own that section oh,

' I ' Code is three bytes, respectively, E6, 88, 91, if you put your birthday can be made out of what word, think is not still a little excited,

For example you are 1988-9-4 born, that corresponds to their own three bytes for E9, 88, 94,anyway This method you can also point yourself,

Explode a material, according to this method, my word is ' glaze ', good word ah, you Spacek make a noise.

In accordance with this law why not everyone has it, then read the Utf-8 binary storage rules to know,

Haha, or dot here character code

Pulling a bunch of useless, is actually hoping that you find coding fun


DIY:

Long ago there was no mb_substr function, so string truncation with Chinese characters is cumbersome to handle, but can now be used directly.

Now that we have a good understanding of the character encoding and the Ord function, we write a function for UTF-8 encoded string truncation.

Code is very poke, to be optimized, but easy to understand, paste the past can be run, the basic scene is also taken into account, but also gratified;

 127) {//non-ASCII code when if ($cnt >= $start) {//If the counter goes to the position of $start $res. = $str [$i]. $str [ + + $i]. $str [+ + $i];              Utf-8 is three-byte encoding, $i the pointer to go three, save the characters $t + +;               Counter + +, indicating that I have saved a few strings to the number of $num to quit the}else{$i + +;                   If you do not go to the $start position, then only walk $i pointer, the character is not processed $i + +;            } $cnt + +;                    }else{if ($cnt >= $start) {//acsii Code normal processing is good $res. = $str [$i];                   $t + +;                                    } $cnt + +;       } if ($num = = $t) break;    OK, I'm going to intercept the number already enough, I'm not greedy, I quit} return $res;   } var_dump (Utf8_substr ($a, 3, 10)); The result should be what you want?>
  • Related Article

    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.