·ÖÏíweb¿ª·¢ÖªÊ¶

×¢²á/µÇ¼|×î½ü·¢²¼|½ñÈÕÍÆ¼ö

Ö÷Ò³ ITÖªÊ¶ÍøÒ³¼¼ÊõÈí¼þ¿ª·¢Ç°¶Ë¿ª·¢´úÂë±à³ÌÔËӪά»¤¼¼Êõ·ÖÏí½Ì³Ì°¸Àý
µ±Ç°Î»ÖãºÊ×Ò³ > ¼¼Êõ·ÖÏí

phpÖÐ ord() ºÍ chr() ´¦Àí utf8 ×Ö·û´®

·¢²¼Ê±¼ä£º2023-09-06 02:10ÔðÈα༭£ººúСº£¹Ø¼ü´Ê£ºÔÝÎÞ±êÇ©


ord()  : ½«×Ö·ûתΪ ascii Âë £¨0-256£©£¬×¼È·¶¨Òå¹Ù·½Îĵµ£ºhttp://php.net/manual/en/function.ord.php

chr() £º ½« ascii ÂëתΪ×Ö·û£¬Óë ord() Ïà·´

²»¹ýÕâÁ½¸ö·½·¨¶¼ÓÐÏÞÖÆ£¬Ö»ÄÜ´¦Àí ascii Â룬

Èç¹ûÐèÒª´¦ÀíµÄ×Ö·û´®°üº¬ utf-8 £¬¾Í²»ÄÜÕý³£¹¤×÷£¬¼ÙÈç ÄãʹÓÃµÄ php ÊÇ´óÓÚ 7.2.0 °æ±¾£¬ÄÇôÄã¿ÉÒÔʹÓÃÔöÇ¿µÄ·½·¨£º

mb_ord()

mb_chr()

Èç¹ûʹÓÃµÄ php °æ±¾Ð¡ÓÚ 7.2.0£¬¿ÉÒÔ³¢ÊÔʹÓÃÒÔÏ·½·¨£º

ord_utf8()

¡¡¡¡function ord_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 ++; ???????????????$code2 = ord(substr($string, $offset, 1)) - 128; ???????//10xxxxxx ???????????????$code_temp = $code_temp*64 + $code2; ???????????} ???????????$code = $code_temp; ???????} ???????$offset += 1; ???????if ($offset >= strlen($string)) $offset = -1; ???????return $code; ???}

Ó÷¨£º

<?php$text = "abc¨¤¨º?€abc";$offset = 0; //$offset is a reference, as it is 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()

 ????function chr_utf8 ($codes) { ???????if (is_scalar($codes)) $codes= func_get_args(); ???????$str= ¡®¡®; ???????foreach ($codes as $code) $str.= html_entity_decode(¡®&#¡®.$code.¡®;¡®,ENT_NOQUOTES,¡®UTF-8¡®); ???????return $str; ???}

Ó÷¨ºÍ chr() Ò»Ñù£»

²Î¿¼£ºhttp://php.net/manual/en/function.ord.php
           http://php.net/manual/en/function.chr.php

ps: ÕâÁ½¸ö·½·¨¶¼ÊDZ¾È˲鿴¹Ù·½ÎĵµÊ±£¬·­ÔÄÒ³Ãæµ×²¿µÄ Óû§¹±Ï× Ëù¿´µ½µÄ£¬½¨ÒéÆ½Ê±²é¿´¹Ù·½ÎĵµÊ±¶àÁôÒâµ×²¿µÄ Óû§¹±Ï×
 

phpÖÐ ord() ºÍ chr() ´¦Àí utf8 ×Ö·û´®

Ô­ÎĵØÖ·£ºhttps://www.cnblogs.com/tommy-huang/p/9490976.html

ÖªÊ¶ÍÆ¼ö

Îҵıà³ÌÑ§Ï°Íø¡ª¡ª·ÖÏíwebǰ¶Ëºó¶Ë¿ª·¢¼¼Êõ֪ʶ¡£ À¬»øÐÅÏ¢´¦ÀíÓÊÏä tousu563@163.com ÍøÕ¾µØÍ¼
icp±¸°¸ºÅ ÃöICP±¸2023006418ºÅ-8 ²»Á¼ÐÅÏ¢¾Ù±¨Æ½Ì¨ »¥ÁªÍø°²È«¹ÜÀí±¸°¸ Copyright 2023 www.wodecom.cn All Rights Reserved