2016-12-28 2 views
1

このコードをテストするとエコーだけです97 whileループでshow 97 98 99 25552 114 21734をループしたいのですが、どうすればいいですか?phpが文字セットを表示するときにどのようにループするのですか?

<?php 
$utf8Character = 'abc提r哦'; 
list(, $ord) = unpack('N', mb_convert_encoding($utf8Character, 'UCS-4BE', 'UTF-8')); 
echo $ord; 
?> 

その後、私は仕事、このコードに変更はなく、それは私がwhileループでショー97 98 99 25552 114 21734のために行うことができますどのように97 98 99 63 63 63 114 63 63 63

<?php 
$utf8Character = 'abc提r哦'; 
for ($i = 0; $i < strlen($utf8Character); $i++) { 
    list(, $ord) = unpack('N', mb_convert_encoding($utf8Character[ $i ], 'UCS-4BE', 'UTF-8')); 
    echo $ord." "; 
} 
?> 

を示しですか?

+0

あなたが作業している文書のエンコーディングは何ですか? UCS-4BE? –

+0

私はエンコーディングUTF-8を使用しています。 –

+0

'mb_strlen()'を使うことができます。 –

答えて

0

strlen()は実際に文字列のバイト数をカウントするため、文字を繰り返し処理しようとすると失敗します。したがって、$utf8Character[$i]を使用すると、漢字はUTF-8の3バイトで構成されるため、漢字は一切取得されません。代わりに、バイトのうちの1つを取得します。

これを修正するには、mb_strlen()を使用してマルチバイト設定の文字数をカウントできます。これはエンコーディングを考慮し、文字列の場合は6を返します。

<?php 

$utf8Character = 'abc提r哦'; 

for ($i = 0; $i < mb_strlen($utf8Character); $i++) { 

    // Get the substring consisting of a single character, 
    // which might consist of multiple bytes. 
    $c = mb_substr($utf8Character, $i, 1); 

    // Convert the character to UCS-4BE, to get the Unicode code point 
    list(, $ord) = unpack('N', mb_convert_encoding($c, 'UCS-4BE', 'UTF-8')); 

    // Print it 
    echo $ord . " "; 
} 

?> 
0

投稿したコードの最初の断片にはいくつかのエラーがあります。 の識別および下記それらを修正してみましょう:

$utf8 = 'abc提r哦'; 
// Put the UCS string into a variable for easier handling 
$ucs = mb_convert_encoding($utf8, 'UCS-4BE', 'UTF-8'); 
echo ($ucs."\n"); 

// Error #1: 'N' unpacks a single 32-bit integer 
print_r(unpack('N', $ucs)); 
// It prints: 
// Array 
// (
//  [1] => 97 
// ) 

// Fix: 'N*' unpacks as many 32-bit integers it can find in the input 
print_r(unpack('N*', $ucs)); 
// It prints: 
// Array 
// (
//  [1] => 97 
//  [2] => 98 
//  [3] => 99 
//  [4] => 25552 
//  [5] => 114 
//  [6] => 21734 
// ) 

// Error #2: list(, $ord) ignores everything starting with index 2 
list(, $ord) = unpack('N*', $ucs); 
var_dump($ord); 
// It prints: 
//  int(97) 

ソリューションは、上記のコードから抽出することができます。

$utf8 = 'abc提r哦'; 
$ucs = mb_convert_encoding($utf8, 'UCS-4BE', 'UTF-8'); 

foreach (unpack('N*', $ucs) as $ord) { 
    echo($ord."\n"); 
} 
関連する問題