2012-04-03 11 views

答えて

5

How to get code point number for a given character in a utf-8 string?私は一緒にこの機能を置く:

だから、
function utf8_to_unicode($str) { 

    $unicode = array();   
    $values = array(); 
    $lookingFor = 1; 

    for ($i = 0; $i < strlen($str); $i++) { 

     $thisValue = ord($str[$i]); 

     if ($thisValue < 128) 
      $unicode[] = str_pad(dechex($thisValue), 4, "0", STR_PAD_LEFT); 
     else { 
      if (count($values) == 0) $lookingFor = ($thisValue < 224) ? 2 : 3;     
      $values[] = $thisValue;     
      if (count($values) == $lookingFor) { 
       $number = ($lookingFor == 3) ? 
       (($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64): 
       (($values[0] % 32) * 64) + ($values[1] % 64); 
       $number = strtoupper(dechex($number)); 
       $unicode[] = str_pad($number, 4, "0", STR_PAD_LEFT); 
       $values = array(); 
       $lookingFor = 1; 
      } // if 
     } // if 
    } // for 
    return ($unicode); 
} // utf8_to_unicode 

$greekString = "ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ "; 
$hexArray = utf8_to_unicode($greekString); 
echo implode("", $hexArray); 

ウィル出力:

039103920393039403950396039703980399039A039B039C039D039E039F03A003A103A303A403A503A603A703A803A90032 
+0

ケニーあなたのコードに何か問題があります。おそらくif($ thisValue <128) $ unicode [] = str_pad($ thisValue、4、 "0"、STR_PAD_LEFT); –

+0

Γιώργοανστείλειςαυτόακριβώςこの試験 右convertion = 0393 03B9 03CE 03C1 03B3 03BF 0032 03B1 03BD 0032 03C3 03C4 03B1 03C5 03C4 03CC 0032 03B1 03BA 03C1 03B9 03B2 03CE 03C2 0032 0116 03B5 03AF 03BB 03B5 03B9 03C2 0032であります0104 0105 0115 0032 0105 0115 0117 0116 0126 0101 0115 0116 あなたのスクリプト= 0393 03B9 03CE 03C1 03B3 03BF 0020 03B1 03BD 0020 03C3 03C4 03B5 03AF 03BB 03B1 03B9 03C2 0020 03B1 03C5 03C4 03CC 0020 03B1 03BA 03C1 03B9 03B2 03CE 03C2 0020 0074 0068 0069 0073 0020 0069 007300000000 0074 0065 0073 0074 –

+0

問題は、AZ、スペースなどのギリシャ文字以外のものです –

0

これは、私のために働いています:

header('Content-Type: text/html; charset=utf-8'); 

bin2hex(iconv('UTF-8', 'UTF-16BE', 'your message')); 
関連する問題