php中utf-8转unicode

<div class="cnblogs_code">

  utf8_unicode( =  =  = 1</span><span style="color: #0000ff;"&gt;for</span> (<span style="color: #800080;"&gt;$i</span> = 0; <span style="color: #800080;"&gt;$i</span> < <span style="color: #008080;"&gt;strlen</span>( <span style="color: #800080;"&gt;$str</span> ); <span style="color: #800080;"&gt;$i</span>++<span style="color: #000000;"&gt; ) {
    </span><span style="color: #800080;"&gt;$thisValue</span> = <span style="color: #008080;"&gt;ord</span>( <span style="color: #800080;"&gt;$str</span>[ <span style="color: #800080;"&gt;$i</span><span style="color: #000000;"&gt; ] );
    </span><span style="color: #0000ff;"&gt;if</span> ( <span style="color: #800080;"&gt;$thisValue</span> < <span style="color: #008080;"&gt;ord</span>('A'<span style="color: #000000;"&gt;) ) {
        </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; exclude 0-9</span>
        <span style="color: #0000ff;"&gt;if</span> (<span style="color: #800080;"&gt;$thisValue</span> >= <span style="color: #008080;"&gt;ord</span>('0') &amp;&amp; <span style="color: #800080;"&gt;$thisValue</span> <= <span style="color: #008080;"&gt;ord</span>('9'<span style="color: #000000;"&gt;)) {
             </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; number</span>
             <span style="color: #800080;"&gt;$unicode</span>[] = <span style="color: #008080;"&gt;chr</span>(<span style="color: #800080;"&gt;$thisValue</span><span style="color: #000000;"&gt;);
        }
        </span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt; {
             </span><span style="color: #800080;"&gt;$unicode</span>[] = '%'.<span style="color: #008080;"&gt;dechex</span>(<span style="color: #800080;"&gt;$thisValue</span><span style="color: #000000;"&gt;);
        }
    } </span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt; {
        </span><span style="color: #0000ff;"&gt;if</span> ( <span style="color: #800080;"&gt;$thisValue</span> < 128<span style="color: #000000;"&gt;) {
            </span><span style="color: #800080;"&gt;$unicode</span>[] = <span style="color: #800080;"&gt;$str</span>[ <span style="color: #800080;"&gt;$i</span><span style="color: #000000;"&gt; ];
        } </span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt; {
            </span><span style="color: #0000ff;"&gt;if</span> ( <span style="color: #008080;"&gt;count</span>( <span style="color: #800080;"&gt;$values</span> ) == 0<span style="color: #000000;"&gt; ) {
                </span><span style="color: #800080;"&gt;$lookingFor</span> = ( <span style="color: #800080;"&gt;$thisValue</span> < 224 ) ? 2 : 3<span style="color: #000000;"&gt;;
            }
            </span><span style="color: #800080;"&gt;$values</span>[] = <span style="color: #800080;"&gt;$thisValue</span><span style="color: #000000;"&gt;;
            </span><span style="color: #0000ff;"&gt;if</span> ( <span style="color: #008080;"&gt;count</span>( <span style="color: #800080;"&gt;$values</span> ) == <span style="color: #800080;"&gt;$lookingFor</span><span style="color: #000000;"&gt; ) {
                </span><span style="color: #800080;"&gt;$number</span> = ( <span style="color: #800080;"&gt;$lookingFor</span> == 3 ) ?<span style="color: #000000;"&gt;
                    ( ( </span><span style="color: #800080;"&gt;$values</span>[0] % 16 ) * 4096 ) + ( ( <span style="color: #800080;"&gt;$values</span>[1] % 64 ) * 64 ) + ( <span style="color: #800080;"&gt;$values</span>[2] % 64 ):<span style="color: #000000;"&gt;
                    ( ( </span><span style="color: #800080;"&gt;$values</span>[0] % 32 ) * 64 ) + ( <span style="color: #800080;"&gt;$values</span>[1] % 64<span style="color: #000000;"&gt; );
                </span><span style="color: #800080;"&gt;$number</span> = <span style="color: #008080;"&gt;dechex</span>(<span style="color: #800080;"&gt;$number</span><span style="color: #000000;"&gt;);
                </span><span style="color: #800080;"&gt;$unicode</span>[] = (<span style="color: #008080;"&gt;strlen</span>(<span style="color: #800080;"&gt;$number</span>)==3)?"\u0".<span style="color: #800080;"&gt;$number</span>:"\u".<span style="color: #800080;"&gt;$number</span><span style="color: #000000;"&gt;;
                </span><span style="color: #800080;"&gt;$values</span> = <span style="color: #0000ff;"&gt;array</span><span style="color: #000000;"&gt;();
                </span><span style="color: #800080;"&gt;$lookingFor</span> = 1<span style="color: #000000;"&gt;;
            } </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; if</span>
        } <span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; if</span>

<span style="color: #000000;"> }
} <span style="color: #008000;">//<span style="color: #008000;"> for
<span style="color: #0000ff;">return <span style="color: #008080;">implode("",<span style="color: #800080;">$unicode<span style="color: #000000;">);
}

参考文章PHP_and_unicode" target="_blank">http://randomchaos.com/documents/?source=PHP_and_unicode

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...