Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 26th, 2012  |  syntax: None  |  size: 0.91 KB  |  views: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. <?php
  3. var_dump(mb_str_split('hello 小姐 韩语中字全集'));
  4. var_dump(utf8_split('hello 小姐 韩语中字全集'));
  5. var_dump(utf8_split_by_mb('hello 小姐 韩语中字全集'));
  6.  
  7.  
  8. function utf8_split($str){
  9.         $bin11 = 0xC0;
  10.         $bin10 = 0x80;
  11.        
  12.         $ch11 = chr($bin11);
  13.         $ch10 = chr($bin10);
  14.        
  15.         $data = array();
  16.        
  17.         $str_len = strlen($str); //str length
  18.         $pos = -1; //postion
  19.        
  20.         for($i=0; $i<$str_len; $i++){
  21.                 if(($str{$i} & $ch11) != $ch10){
  22.                         ++$pos;
  23.                 }
  24.                 isset($data[$pos]) or $data[$pos] = '';
  25.                 $data[$pos] .= $str{$i};
  26.         }
  27.        
  28.         return $data;
  29. }
  30.  
  31. function mb_str_split( $string ) {
  32.     # Split at all position not after the start: ^
  33.     # and not before the end: $
  34.     return preg_split('/(?<!^)(?!$)/u', $string );
  35. }
  36.  
  37. function utf8_split_by_mb($string){
  38.         $stop   = mb_strlen( $string);
  39.         $result = array();
  40.  
  41.         for( $idx = 0; $idx < $stop; $idx++)
  42.         {
  43.            $result[] = mb_substr( $string, $idx, 1);
  44.         }
  45.        
  46.         return $result;
  47. }
  48.  
  49. ?>