0

文字 128 ~ 255 など、PHP で非標準文字の文字列を作成し、その文字列を CP1252 としてエンコードしたいと考えています。

<?php
$cp1252 = '';

for ($i = 128; $i < 256; $i++) {
    $hex = dechex($i);
    $cp1252 .= "\x$hex";
}

echo $cp1252;

変数が初期化される前にエスケープシーケンスが解析されるため、これが機能しないことはわかっていました(間違っている場合は修正してください)。これは、私がやりたいことの例として役立ちます。

これは、CP1252 から UTF-8 への変換をテストするために使用した最終的なコードです。

<?php
$cp1252 = '';

for ($i = 128; $i < 256; $i++) {
    $cp1252 .= chr($i);
}

echo iconv("CP1252", "UTF-8", $cp1252);
4

2 に答える 2

4

関数を使用してchr()、文字コードを文字列に変換します。

for ($i = 128; $i < 256; $i++) {
    $cp1252 .= chr($i);
}
于 2013-10-23T21:12:50.567 に答える
1

To generate a string of random characters:

function rand_cp1252($length) {
  $ostr = '';
  for($i=0;$i<$length; $i++) {
    $ostr .= chr(rand(128,255));
  }
  return $ostr;
}

echo rand_cp1252(10);

As far as the 'encoding' goes that has nothing to do with the string itself, you want to make sure you're setting the correct headers for the encoding type when you serve the data.

于 2013-10-23T22:13:07.290 に答える