Skip to content

Instantly share code, notes, and snippets.

@wjzhangq
Created October 22, 2010 07:23
Show Gist options
  • Save wjzhangq/640093 to your computer and use it in GitHub Desktop.
Save wjzhangq/640093 to your computer and use it in GitHub Desktop.
<?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;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment