Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- var_dump(mb_str_split('hello 小姐 韩语中字全集'));
- var_dump(utf8_split('hello 小姐 韩语中字全集'));
- var_dump(utf8_split_by_mb('hello 小姐 韩语中字全集'));
- function utf8_split($str){
- $bin11 = 0xC0;
- $bin10 = 0x80;
- $ch11 = chr($bin11);
- $ch10 = chr($bin10);
- $data = array();
- $str_len = strlen($str); //str length
- $pos = -1; //postion
- for($i=0; $i<$str_len; $i++){
- if(($str{$i} & $ch11) != $ch10){
- ++$pos;
- }
- isset($data[$pos]) or $data[$pos] = '';
- $data[$pos] .= $str{$i};
- }
- return $data;
- }
- function mb_str_split( $string ) {
- # Split at all position not after the start: ^
- # and not before the end: $
- return preg_split('/(?<!^)(?!$)/u', $string );
- }
- function utf8_split_by_mb($string){
- $stop = mb_strlen( $string);
- $result = array();
- for( $idx = 0; $idx < $stop; $idx++)
- {
- $result[] = mb_substr( $string, $idx, 1);
- }
- return $result;
- }
- ?>
Add Comment
Please, Sign In to add comment