Maison  >  Article  >  développement back-end  >  php中utf-8转unicode

php中utf-8转unicode

WBOY
WBOYoriginal
2016-08-08 09:32:571039parcourir
<span>public</span> <span>function</span> utf8_unicode(<span>$str</span><span>) {
    </span><span>$unicode</span> = <span>array</span><span>();
    </span><span>$values</span> = <span>array</span><span>();
    </span><span>$lookingFor</span> = 1<span>;

    </span><span>for</span> (<span>$i</span> = 0; <span>$i</span> strlen( <span>$str</span> ); <span>$i</span>++<span> ) {
        </span><span>$thisValue</span> = <span>ord</span>( <span>$str</span>[ <span>$i</span><span> ] );
        </span><span>if</span> ( <span>$thisValue</span> ord('A'<span>) ) {
            </span><span>//</span><span> exclude 0-9</span>
            <span>if</span> (<span>$thisValue</span> >= <span>ord</span>('0') && <span>$thisValue</span> ord('9'<span>)) {
                 </span><span>//</span><span> number</span>
                 <span>$unicode</span>[] = <span>chr</span>(<span>$thisValue</span><span>);
            }
            </span><span>else</span><span> {
                 </span><span>$unicode</span>[] = '%'.<span>dechex</span>(<span>$thisValue</span><span>);
            }
        } </span><span>else</span><span> {
            </span><span>if</span> ( <span>$thisValue</span> ) {
                <span>$unicode</span>[] = <span>$str</span>[ <span>$i</span><span> ];
            } </span><span>else</span><span> {
                </span><span>if</span> ( <span>count</span>( <span>$values</span> ) == 0<span> ) {
                    </span><span>$lookingFor</span> = ( <span>$thisValue</span> ;
                }
                <span>$values</span>[] = <span>$thisValue</span><span>;
                </span><span>if</span> ( <span>count</span>( <span>$values</span> ) == <span>$lookingFor</span><span> ) {
                    </span><span>$number</span> = ( <span>$lookingFor</span> == 3 ) ?<span>
                        ( ( </span><span>$values</span>[0] % 16 ) * 4096 ) + ( ( <span>$values</span>[1] % 64 ) * 64 ) + ( <span>$values</span>[2] % 64 ):<span>
                        ( ( </span><span>$values</span>[0] % 32 ) * 64 ) + ( <span>$values</span>[1] % 64<span> );
                    </span><span>$number</span> = <span>dechex</span>(<span>$number</span><span>);
                    </span><span>$unicode</span>[] = (<span>strlen</span>(<span>$number</span>)==3)?"\u0".<span>$number</span>:"\u".<span>$number</span><span>;
                    </span><span>$values</span> = <span>array</span><span>();
                    </span><span>$lookingFor</span> = 1<span>;
                } </span><span>//</span><span> if</span>
            } <span>//</span><span> if</span>
<span>        }
    } </span><span>//</span><span> for</span>
    <span>return</span> <span>implode</span>("",<span>$unicode</span><span>);
}</span>

参考文章:http://randomchaos.com/documents/?source=php_and_unicode

以上就介绍了php中utf-8转unicode,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn