mysql utf8 汉字转拼音_php mysql中utf8编码汉字转换成拼音

这段代码展示了如何使用PHP处理GBK编码到UTF-8的转换,并获取字符串的拼音。通过连接MySQL数据库,更新表中记录的标题为拼音,实现了数据库中的编码管理和搜索优化。此外,还提供了一个RPC服务,根据用户输入进行模糊匹配搜索。
摘要由CSDN通过智能技术生成

例子,php mysql实现编码转换。 代码示例:

require_once('pinyin_table.php');

function get_pinyin_array($string)

{

global $pinyin_table;

$flow = array();

for ($i=0;$i

{

if (ord($string[$i]) >= 0x81 and ord($string[$i]) <= 0xfe)

{

$h = ord($string[$i]);

if (isset($string[$i+1]))

{

$i++;

$l = ord($string[$i]);

if (isset($pinyin_table[$h][$l]))

{

array_push($flow,$pinyin_table[$h][$l]);

}

else

{

array_push($flow,$h);

array_push($flow,$l);

}

}

else

{

array_push($flow,ord($string[$i]));

}

}

else

{

array_push($flow,ord($string[$i]));

}

}

//print_r($flow);

$pinyin = array();

$pinyin[0] = '';

for ($i=0;$i

{

if (is_array($flow[$i]))

{

if (sizeof($flow[$i]) == 1)

{

foreach ($pinyin as $key => $value)

{

$pinyin[$key] .= $flow[$i][0];

}

}

if (sizeof($flow[$i]) > 1)

{

$tmp1 = $pinyin;

foreach ($pinyin as $key => $value)

{

$pinyin[$key] .= $flow[$i][0];

}

for ($j=1;$j

{

$tmp2 = $tmp1;

for ($k=0;$k

{

$tmp2[$k] .= $flow[$i][$j];

}

array_splice($pinyin,sizeof($pinyin),0,$tmp2);

}

}

}

else

{

foreach ($pinyin as $key => $value)

{

$pinyin[$key] .= chr($flow[$i]);

}

}

}

return $pinyin;

}

function GetGB2312String($name)

{

$tostr = "";

for($i=0;$i

{

$curbin = ord(substr($name,$i,1));

if($curbin < 0x80)

{

$tostr .= substr($name,$i,1);

}elseif($curbin < bindec("11000000")){

$str = substr($name,$i,1);

$tostr .= "".ord($str).";";

}elseif($curbin < bindec("11100000")){

$str = substr($name,$i,2);

$tostr .= "".GetUnicodeChar($str).";";

$i += 1;

}elseif($curbin < bindec("11110000")){

$str = substr($name,$i,3);

$gstr= iconv("UTF-8","GB2312",$str);

if(!$gstr)

{

$tostr .= "".GetUnicodeChar($str).";";

}else{

$tostr .= $gstr;

}

$i += 2;

}elseif($curbin < bindec("11111000")){

$str = substr($name,$i,4);

$tostr .= "".GetUnicodeChar($str).";";

$i += 3;

}elseif($curbin < bindec("11111100")){

$str = substr($name,$i,5);

$tostr .= "".GetUnicodeChar($str).";";

$i += 4;

}else{

$str = substr($name,$i,6);

$tostr .= "".GetUnicodeChar($str).";";

$i += 5;

}

}

return $tostr;

}

function GetUnicodeChar($str)

{

$temp = "";

for($i=0;$i

{

$x = decbin(ord(substr($str,$i,1)));

if($i == 0)

{

$s = strlen($str)+1;

$temp .= substr($x,$s,8-$s);

}else{

$temp .= substr($x,2,6);

}

}

return bindec($temp);

}

$db = mysql_connect("127.0.0.1:3307", "root","123");

mysql_select_db("qq",$db);

$result = mysql_query("set names utf8",$db);

$result = mysql_query("select title,group_id from groups",$db);

while ($myrow = mysql_fetch_array($result)) {

$text1="$myrow[0]";

$text= GetGB2312String($text1);//获得GB2312编码串

$flow = get_pinyin_array($text);//获得拼音

$result2 = mysql_query("update groups set titlepinyin='$flow[0]' where group_id = $myrow[1]",$db);

printf("%s
%s
",$text1,$flow[0]);

}

?>

表中加入一个字段titlepinyin 代码示例:

mysql>alter table groups add titlepinyin varchar(1000);

rpc.php改为 代码示例:

$db = mysql_connect("127.0.0.1:3307", "root","123");

if(!$db) {

// Show error if we cannot connect.

echo 'ERROR: Could not connect to the database.';

} else {

// Is there a posted query string?

if(isset($_POST['queryString'])) {

$queryString = $_POST['queryString'];

$length=strlen($queryString);

mysql_select_db("qq",$db);

$result = mysql_query("set names utf8");

// Is the string length greater than 0?

if($length>0) { (脚本学堂 www.# 编辑整理)

$sql="";

if(!ereg("^[A-Za-z0-9_.-]+$",$queryString))//如果有汉字的话

{$sql="SELECT title FROM groups WHERE title LIKE '$queryString%' LIMIT 10";

}else{//英文数字符号

$sql="SELECT title FROM groups WHERE titlepinyin LIKE '$queryString%' LIMIT 10";

}

$query = mysql_query($sql);

// Run the query: We use LIKE ‘$queryString%’

if($query) {

while ($myrow = mysql_fetch_array($query))  {

// Format the results, im using

for the list, you can change it.

// The onClick function fills the textbox with the result.

echo '

'.$myrow[0].'';

}

} else {

echo "ERROR: There was a problem with the query $sql.";

}

} else {

}

} else {

echo 'There should be no direct access to this script!';

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值