Home >php教程 >PHP源码 >把汉字转化为拼音,(最笨也是最准确的办法)

把汉字转化为拼音,(最笨也是最准确的办法)

PHP中文网
PHP中文网Original
2016-05-25 17:07:301198browse

  

2. [代码][PHP]代码     跳至                        

<?php  
/**  
	*  本文作者:chinesehero@163.com
	*  获取一个汉字的拼音  
	*  用法如下:
	*  include(&#39;Pinyin.php&#39;);
	*  echo getPinyin("你好,吃了吗?");
    */
      
    function  getPinyin($keyWord)  
    {  
		$hz[&#39;腌&#39;]="yan";
		$hz[&#39;嗄&#39;]="a";
		$hz[&#39;迫&#39;]="po";
		$hz[&#39;捱&#39;]="ai";
		$hz[&#39;艾&#39;]="ai";
	//此处省略6900余字
		$hz[&#39;蜱&#39;]="pi";
		$hz[&#39;螋&#39;]="sou";
		$hz[&#39;螗&#39;]="tang";
		$hz[&#39;螵&#39;]="piao";
		$hz[&#39;蟛&#39;]="peng";

		
		$result="";
		$charArray=str_split_php5_utf8($keyWord);
		foreach($charArray as $char){
		if(!empty($hz[$char])){
			$result= $result.$hz[$char];
		}
		else
		{
			$result= $result.$char;
		}
		}
		return $result;
	}
	
	function str_split_php5_utf8($str) { 
    // place each character of the string into and array 
    $split=1; 
    $array = array(); 
    for ( $i=0; $i < strlen( $str ); ){ 
        $value = ord($str[$i]); 
        if($value > 127){ 
            if($value >= 192 && $value = 224 && $value = 240 && $value <= 247) 
                $split=4; 
        }else{ 
            $split=1; 
        } 
            $key = NULL; 
        for ( $j = 0; $j < $split; $j++, $i++ ) { 
            $key .= $str[$i]; 
        } 
        array_push( $array, $key ); 
    } 
    return $array; 
	}
   
?>

           

       

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn