SSブログ
最近PHPでいろいろ作ってます。もし良ければ見ていってください。
メーリングリスト → http://www.easyml.info/
twitm -メール de twitter- → http://www.twitm.net/

続・PHPで絵文字変換 [プログラミング]

 「KDDI絵文字用Shift-JISコード」と「メール送出用JISコードに対応したShift-JISコード」の変換処理を再考してみました。

 当たり前といえば当たり前な処理ですが。

1.文字列を1文字ずつに分解。
2.対象となる絵文字コードを置換。
3.分解した文字列を再結合。

 まあこうする以外ないでしょう。
 でも、1文字ずつループして判定というのはやはりパフォーマンスが心配です。

 で、いろいろ考えた結果こうなりました。

    $string =”絵文字入り文字列";
// 文字列分解
$ascii = '([\x00-\x7F\xA1-\xDF])';
$twoBytes = '([\x81-\x9F\xE0-\xEF][\x40-\x7E\x80-\xFC])';
preg_match_all("/$ascii|$twoBytes/", $string, $matches);
$element = $matches[0];

$patterns[0] = '/\xEB([\x40-\x7E\x80-\xFC])/';
$patterns[1] = '/\xEC([\x40-\x7E\x80-\xFC])/';
$patterns[2] = '/\xED([\x40-\x7E\x80-\xFC])/';
$patterns[3] = '/\xEE([\x40-\x7E\x80-\xFC])/';
$replacements[0] = "\xF6\\1";
$replacements[1] = "\xF7\\1";
$replacements[2] = "\xF3\\1";
$replacements[3] = "\xF4\\1";
ksort($patterns);
ksort($replacements);

// 文字単位で置換
$element_new = preg_replace( $patterns, $replacements, $element );

// 文字列再結合
$string = implode("", $element_new);



たぶんこれでそこそこのパフォーマンスでちゃんと変換できると思うのですが・・・どうですかね?
nice!(0)  コメント(1)  トラックバック(0) 
共通テーマ:携帯コンテンツ

nice! 0

コメント 1

Tam

Howdy I am so glad I found your web site, I really found you
by accident, while I was researching on Digg for something else, Anyways I am here now and would just
like to say thanks for a fantastic post and a all round enjoyable blog
(I also love the theme/design), I don’t have time to look over it all at the moment but I
have book-marked it and also added in your RSS feeds, so
when I have time I will be back to read more, Please do keep up
the superb jo.
by Tam (2018-03-22 05:40) 

コメントを書く

お名前:[必須]
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

トラックバック 0

記念硬貨6960円 ブログトップ

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。