代码很简单,功能却很实用,推荐给大家。
奉上代码先:
<br />public function utf8_unicode($str) {<br /> $unicode = array();<br /> $values = array();<br /> $lookingFor = 1;<br /> for ($i = 0; $i < strlen( $str ); $i++ ) {<br /> $thisValue = ord( $str[ $i ] );<br /> if ( $thisValue < ord('A') ) {<br /> // exclude 0-9<br /> if ($thisValue >= ord('0') && $thisValue <= ord('9')) {<br /> // number<br /> $unicode[] = chr($thisValue);<br /> }<br /> else {<br /> $unicode[] = '%'.dechex($thisValue);<br /> }<br /> } else {<br /> if ( $thisValue < 128) {<br /> $unicode[] = $str[ $i ];<br /> } else {<br /> if ( count( $values ) == 0 ) {<br /> $lookingFor = ( $thisValue < 224 ) ? 2 : 3;<br /> }<br /> $values[] = $thisValue;<br /> if ( count( $values ) == $lookingFor ) {<br /> $number = ( $lookingFor == 3 ) ?<br /> ( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 ):<br /> ( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64 );<br /> $number = dechex($number);<br /> $unicode[] = (strlen($number)==3)?"\u0".$number:"\u".$number;<br /> $values = array();<br /> $lookingFor = 1;<br /> } // if<br /> } // if<br /> }<br /> } // for<br /> r<i style="color:transparent">本¥文来源gaodai$ma#com搞$代*码*网(</i><strong>搞代gaodaima码</strong>eturn implode("",$unicode);<br />}<br />