2012-04-03 11 views

Antwort

5

Basierend auf der original code und die Antwort auf diese Frage: How to get code point number for a given character in a utf-8 string? ich zusammen diese Funktion:

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 

So:

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

Ausgang Will:

039103920393039403950396039703980399039A039B039C039D039E039F03A003A103A303A403A503A603A703A803A90032 
+0

Kenny, es ist etwas falsch mit Ihrem Code. höchstwahrscheinlich bei if ($ thisValue <128) $ unicode [] = str_pad ($ thisValue, 4, "0", STR_PAD_LEFT); –

+0

Γιώργο αν στείλεις αυτό ακριβώς Dies ist ein Test Die rechte convertion = 0393 03B9 03CE 03C1 03B3 03BF 0032 03B1 03BD 0032 03C3 03C4 03B5 03AF 03BB 03B5 03B9 03C2 0032 03B1 03C5 03C4 03CC 0032 03B1 03BA 03C1 03B9 03B2 03CE 03C2 0032 0116 0104 0105 0115 0032 0105 0115 0032 0097 0032 0116 0101 0115 0116 Ihr script = 0393 03B9 03CE 03C1 03B3 03BF 0020 03B1 03BD 0020 03C3 03C4 03B5 03AF 03BB 03B5 03B9 03C2 0020 03B1 03C5 03C4 03CC 0020 03B1 03BA 03C1 03B9 03B2 03CE 03C2 0020 0074 0068 0069 0073 0020 0000 0073 0020 0061 0020 0074 0065 0073 0074 –

+0

Das Problem ist mit jedem anderen als griechische Zeichen wie AZ, Leerzeichen usw. –

0

Dies ist Arbeiten für mich:

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

bin2hex(iconv('UTF-8', 'UTF-16BE', 'your message')); 
Verwandte Themen