脚本宝典收集整理的这篇文章主要介绍了php – 如何获取utf-8字符串中给定字符的代码点号?,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
所以,问题归结为“将给定的字符转换为UCS-2代码点”
但是怎么样请,非常感谢,因为我很匆忙.
提前致谢
提问者答复的转录作为答案
字符串将是用户输入,从表单字段.
我想实现一个PHP版本的utf8to16或utf8decode like
function get_ucs2_codepoint($char) { // calculation of ucs2 codepoint value and assign IT to $hex_codepoint return $hex_codepoint; }
再次感谢你.
function utf8_to_unicode( $str ) { $unicode = array(); $values = array(); $lookingFor = 1; for ($i = 0; $i < strlen( $str ); $i++ ) { $thisValue = ord( $str[ $i ] ); if ( $thisValue < ord('A') ) { // exclude 0-9 if ($thisValue >= ord('0') && $thisValue <= ord('9')) { // number $unicode[] = chr($thisValue); } else { $unicode[] = '%'.dechex($thisValue); } } else { if ( $thisValue < 128) $unicode[] = $str[ $i ]; else { if ( count( $values ) == 0 ) $lookingFor = ( $thisValue < 224 ) ? 2 : 3; $values[] = $thisValue; if ( count( $values ) == $lookingFor ) { $number = ( $lookingFor == 3 ) ? ( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 ): ( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64 ); $number = dechex($number); $unicode[] = (strlen($number)==3)?"%u0".$number:"%u".$number; $values = array(); $lookingFor = 1; } // if } // if } } // for return implode("",$unicode); } // utf8_to_unicode
以上是脚本宝典为你收集整理的php – 如何获取utf-8字符串中给定字符的代码点号?全部内容,希望文章能够帮你解决php – 如何获取utf-8字符串中给定字符的代码点号?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。