PC2M (Website Transcoder for Mobile Clients) で絵文字を使う
表題のスクリプト。かなり便利なので活用させていただいてます。
コレを自サイトに設置し且つ携帯絵文字も使いたい!
っとなった時の対応メモ。
かなり強引な実装で少々恥ずかしいですが、公開。
使用するライブラリ
- http://surf-style.us/manual2.htm
- 携帯絵文字の変換を行う
- PC2M
- PC向けWebサイトを携帯向けに変換する(キャリア別の対応も)
1) PHP 携帯絵文字 自動変換スクリプト のハック
1. Shift-JISのバイナリコード入力 → Unicodeのテキスト入力
バイナリ入力方式だとうまくいかなかったので。。
function _EmojiTable() { $this->EMOJI['E63E'] = array('TIT' => '晴れ', 'EzWeb' => '44', 'SB' => '^[$Gj^O'); }
2. 出力文字列作成部分のカスタマイズ
Docomo用のimg:attributeを追加定義し、出力は以下の形に。
<img docomosrc="xxxx" />
pc2mに適合させるイメージ。変更箇所は以下。
function Convert($InputEmoji) { switch ($this->InputMode) { case 0: $InputEmoji = strtoupper($InputEmoji); break; case 1: $InputEmoji = strtoupper(bin2hex($InputEmoji)); break; default: return '入力モードの指定が正しくありません。'; break; } if ($this->strUserAgent == null) { getUserAgent(); } switch ($this->strUserAgent) { case 1: // DoCoMo //$InputEmoji = pack("H*",$InputEmoji); $InputEmoji = '<!--pc2m_remain--><img docomosrc="'.$InputEmoji.'"><!--/pc2m_remain-->'; break; case 2: // SoftBank $InputEmoji = $this->EMOJI[$InputEmoji]['SB']; break; case 3: // EzWeb //$InputEmoji = is_numeric($this->EMOJI[$InputEmoji]['EzWeb'])?"<img localsrc=" . $this->EMOJI[$InputEmoji]['EzWeb'] . ">":$this->EMOJI[$InputEmoji]['EzWeb']; $InputEmoji = is_numeric($this->EMOJI[$InputEmoji]['EzWeb'])?'<!--pc2m_remain--><img localsrc="' . $this->EMOJI[$InputEmoji]['EzWeb'] . '"><!--/pc2m_remain-->':$this->EMOJI[$InputEmoji]['EzWeb']; break; case 4: // PC //$InputEmoji = "<img src='./emoji/" . $InputEmoji . ".gif'>"; $InputEmoji = '&#x'.$InputEmoji.';'; break; } return $InputEmoji; }
2) PC2M のハック
1. imgタグのAttributeの拡張
'img' => array( 'type' => 'inline', 'children' => array(), //'attributes' => array('title', 'src', 'alt', 'name', 'height', 'width', 'align', 'border'), 'attributes' => array('title', 'src', 'alt', 'name', 'height', 'width', 'align', 'border', 'docomosrc', 'localsrc'), 'required_attribute' => 'src', 'docomo_attribute' => 'docomosrc', 'au_attribute' => 'localsrc' ),
2.function _checkAttributes($name, &$attribs) のカスタマイズ
function _checkAttributes($name, &$attribs) { if (!empty($attribs)) { ・・・ } // add HERE // case Docomo if (isset($this->dtd[$name]['docomo_attribute']) and isset($attribs[$this->dtd[$name]['docomo_attribute']])) { return true; } // case AU if (isset($this->dtd[$name]['au_attribute']) and isset($attribs[$this->dtd[$name]['au_attribute']])) { return true; } // END if (isset($this->dtd[$name]['required_attribute']) and !isset($attribs[$this->dtd[$name]['required_attribute']])) { return false; } else { return true; } }
3. 置換部分のカスタマイズ
if (preg_match_all('/<!--pc2m_remain-->(.+?)<!--\/pc2m_remain-->/si', $_content, $matches)) { $limit = count($matches[0]); for ($i = 0; $i < $limit; $i++) { // add HERE if ($docomo_emoji = getAttribute("docomosrc", $matches[1][$i])) { $docomo_emoji = '&#x'.$docomo_emoji.';'; $matches[1][$i] = $docomo_emoji; } // END $_remains[] = $matches[1][$i]; $search[] = $matches[0][$i]; $replace[] = '<!--pc2m_untouch_'.(count($_remains) -1).'-->'; } }
一応はコレで動きます。
最後に
改めてソースを見直して、
pc2mの変換処理をフックしている部分で、絵文字変換処理したほうがお洒落だった気が・・・
と思ったりしている今日この頃。
とはいえとりあえず動いてるし、時間がある時に積み残し。
追伸:
作者さまには色々とアドバイスをいただき非常に助かりました。
ありがとうございます。