php中utf-8转unicode,phputf-8转unicode_PHP教程

WBOY
Lepaskan: 2016-07-13 10:11:59
asal
861 orang telah melayarinya

php中utf-8转unicode,phputf-8转unicode

<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> < <span>strlen</span>( <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> < <span>ord</span>('A'<span>) ) {
            </span><span>//</span><span> exclude 0-9</span>
            <span>if</span> (<span>$thisValue</span> >= <span>ord</span>('0') && <span>$thisValue</span> <= <span>ord</span>('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> < 128<span>) {
                </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> < 224 ) ? 2 : 3<span>;
                }
                </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>
Salin selepas log masuk

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/924849.htmlTechArticlephp中utf-8转unicode,phputf-8转unicode public function utf8_unicode( $str ) { $unicode = array (); $values = array (); $lookingFor = 1 ; for ( $i = 0; $i strlen ( $str ); $i ++...
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!