1. /**
    2. * gbk转拼音
    3. * @param $txt
    4. */
    5. function gbk_to_pinyin($txt) {
    6. if(CHARSET != 'gbk') {
    7. $txt = iconv(CHARSET,'GBK',$txt);
    8. }
    9. $l = strlen($txt);
    10. $i = 0;
    11. $pyarr = array();
    12. $py = array();
    13. $filename = CODETABLEDIR.'gb-pinyin.table';
    14. $fp = fopen($filename,'r');
    15. while(!feof($fp)) {
    16. $p = explode("-",fgets($fp,32));
    17. $pyarr[intval($p[1])] = trim($p[0]);
    18. }
    19. fclose($fp);
    20. ksort($pyarr);
    21. while($i<$l) {
    22. $tmp = ord($txt[$i]);
    23. if($tmp>=128) {
    24. $asc = abs($tmp*256+ord($txt[$i+1])-65536);
    25. $i = $i+1;
    26. } else $asc = $tmp;
    27. $py[] = asc_to_pinyin($asc,$pyarr);
    28. $i++;
    29. }
    30. return $py;
    31. }