Ord () and Chr () handle UTF8 strings in PHP

Source: Internet
Author: User
Tags ord


Ord (): Convert character to ASCII (0-256), define official document exactly: http://php.net/manual/en/function.ord.php

Chr (): Converts ASCII code to character, opposite to Ord ()

However, these two methods are limited, only the ASCII code can be processed,

If you need to handle a string containing utf-8, it will not work, if you are using PHP is greater than 7.2.0, then you can use the Enhanced method:

Mb_ord ()

MB_CHR ()

If you are using a PHP version that is less than 7.2.0, you can try the following methods:

Ord_utf8 ()

functionOrd_utf8 ($string, &$offset) {        $code=Ord(substr($string,$offset, 1)); if($code>= 128) {//otherwise 0xxxxxxx            if($code< 224)$bytes _number= 2;//110xxxxx            Else if($code< 240)$bytes _number= 3;//1110xxxx            Else if($code< 248)$bytes _number= 4;//11110xxx            $code _temp=$code-192-($bytes _number> 2? 32:0)-($bytes _number> 3? 16:0);  for($i= 2;$i<=$bytes _number;$i++) {                $offset++; $code 2=Ord(substr($string,$offset, 1))-128;//10xxxxxx                $code _temp=$code _temp*64 +$code 2; }            $code=$code _temp; }        $offset+ = 1; if($offset>=strlen($string))$offset=-1; return $code; }

Usage:

<? PHP $text = "Abcàê?€abc"; $offset // $offset is a reference, as it was not easy-to-split a utf-8 Char-by-char. Useful to iterate on a string:  while ($offset >= 0) {    echo$offset. ":". Ordutf8 ($text$ Offset). " \ n ";} -----------------------------------------/*  returns:0:971:982:993:2245:2347:2239:836412:9713: 9814:99*/?>

Chr_utf8 ()

     functionChr_utf8 ($codes) {        if(Is_scalar($codes))$codes=Func_get_args(); $str= ' '; foreach($codes  as $code)$str.=Html_entity_decode(' a '.$code. '; ', ent_noquotes, ' UTF-8 '); return $str; }

Use the same as Chr ();

Reference: http://php.net/manual/en/function.ord.php
http://php.net/manual/en/function.chr.php

PS: These two methods are I look at the official documents, read the user contribution at the bottom of the page to see, it is recommended to view the official documents at the end of the user contributions to pay more attention

Ord () and Chr () handle UTF8 strings in PHP

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.