当前位置:Gxlcms > PHP教程 > phpmysql中utf8编码汉字转换成拼音

phpmysql中utf8编码汉字转换成拼音

时间:2021-07-01 10:21:17 帮助过:8人阅读

  1. require_once('pinyin_table.php');

  2. function get_pinyin_array($string)
  3. {
  4. global $pinyin_table;
  5. $flow = array();
  6. for ($i=0;$i {
  7. if (ord($string[$i]) >= 0x81 and ord($string[$i]) <= 0xfe)
  8. {
  9. $h = ord($string[$i]);
  10. if (isset($string[$i+1]))
  11. {
  12. $i++;
  13. $l = ord($string[$i]);
  14. if (isset($pinyin_table[$h][$l]))
  15. {
  16. array_push($flow,$pinyin_table[$h][$l]);
  17. }
  18. else
  19. {
  20. array_push($flow,$h);
  21. array_push($flow,$l);
  22. }
  23. }
  24. else
  25. {
  26. array_push($flow,ord($string[$i]));
  27. }
  28. }
  29. else
  30. {
  31. array_push($flow,ord($string[$i]));
  32. }
  33. }
  34. //print_r($flow);
  35. $pinyin = array();
  36. $pinyin[0] = '';
  37. for ($i=0;$i {
  38. if (is_array($flow[$i]))
  39. {
  40. if (sizeof($flow[$i]) == 1)
  41. {
  42. foreach ($pinyin as $key => $value)
  43. {
  44. $pinyin[$key] .= $flow[$i][0];
  45. }
  46. }
  47. if (sizeof($flow[$i]) > 1)
  48. {
  49. $tmp1 = $pinyin;
  50. foreach ($pinyin as $key => $value)
  51. {
  52. $pinyin[$key] .= $flow[$i][0];
  53. }
  54. for ($j=1;$j {
  55. $tmp2 = $tmp1;
  56. for ($k=0;$k {
  57. $tmp2[$k] .= $flow[$i][$j];
  58. }
  59. array_splice($pinyin,sizeof($pinyin),0,$tmp2);
  60. }
  61. }
  62. }
  63. else
  64. {
  65. foreach ($pinyin as $key => $value)
  66. {
  67. $pinyin[$key] .= chr($flow[$i]);
  68. }
  69. }
  70. }
  71. return $pinyin;
  72. }

  73. function GetGB2312String($name)

  74. {
  75. $tostr = "";
  76. for($i=0;$i {
  77. $curbin = ord(substr($name,$i,1));
  78. if($curbin < 0x80)
  79. {
  80. $tostr .= substr($name,$i,1);
  81. }elseif($curbin < bindec("11000000")){
  82. $str = substr($name,$i,1);
  83. $tostr .= "&#".ord($str).";";
  84. }elseif($curbin < bindec("11100000")){
  85. $str = substr($name,$i,2);
  86. $tostr .= "&#".GetUnicodeChar($str).";";
  87. $i += 1;
  88. }elseif($curbin < bindec("11110000")){
  89. $str = substr($name,$i,3);
  90. $gstr= iconv("UTF-8","GB2312",$str);
  91. if(!$gstr)
  92. {
  93. $tostr .= "&#".GetUnicodeChar($str).";";
  94. }else{
  95. $tostr .= $gstr;
  96. }
  97. $i += 2;
  98. }elseif($curbin < bindec("11111000")){
  99. $str = substr($name,$i,4);
  100. $tostr .= "&#".GetUnicodeChar($str).";";
  101. $i += 3;
  102. }elseif($curbin < bindec("11111100")){
  103. $str = substr($name,$i,5);
  104. $tostr .= "&#".GetUnicodeChar($str).";";
  105. $i += 4;
  106. }else{
  107. $str = substr($name,$i,6);
  108. $tostr .= "&#".GetUnicodeChar($str).";";
  109. $i += 5;
  110. }
  111. }
  112. return $tostr;
  113. }
  114. function GetUnicodeChar($str)
  115. {
  116. $temp = "";
  117. for($i=0;$i {
  118. $x = decbin(ord(substr($str,$i,1)));
  119. if($i == 0)
  120. {
  121. $s = strlen($str)+1;
  122. $temp .= substr($x,$s,8-$s);
  123. }else{
  124. $temp .= substr($x,2,6);
  125. }
  126. }
  127. return bindec($temp);
  128. }

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

  130. mysql_select_db("qq",$db);

  131. $result = mysql_query("set names utf8",$db);
  132. $result = mysql_query("select title,group_id from groups",$db);

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

  134. $text1="$myrow[0]";
  135. $text= GetGB2312String($text1);//获得GB2312编码串
  136. $flow = get_pinyin_array($text);//获得拼音
  137. $result2 = mysql_query("update groups set titlepinyin='$flow[0]' where group_id = $myrow[1]",$db);
  138. printf("%s
    %s
    ",$text1,$flow[0]);
  139. }
  140. ?>

表中加入一个字段titlepinyin

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

rpc.php改为

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

  2. if(!$db) {
  3. // Show error if we cannot connect.
  4. echo 'ERROR: Could not connect to the database.';
  5. } else {
  6. // Is there a posted query string?
  7. if(isset($_POST['queryString'])) {
  8. $queryString = $_POST['queryString'];
  9. $length=strlen($queryString);

  10. mysql_select_db("qq",$db);

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

  12. // Is the string length greater than 0?

  13. if($length>0) { (程序员之家 bbs.it-home.org 编辑整理)
  14. $sql="";
  15. if(!ereg("^[A-Za-z0-9_.-]+$",$queryString))//如果有汉字的话
  16. {$sql="SELECT title FROM groups WHERE title LIKE '$queryString%' LIMIT 10";
  17. }else{//英文数字符号
  18. $sql="SELECT title FROM groups WHERE titlepinyin LIKE '$queryString%' LIMIT 10";
  19. }
  20. $query = mysql_query($sql);
  21. // Run the query: We use LIKE ‘$queryString%’
  22. if($query) {
  23. while ($myrow = mysql_fetch_array($query)) {
  24. // Format the results, im using
  25. for the list, you can change it.
  26. // The onClick function fills the textbox with the result.
  27. echo '
  28. '.$myrow[0].'
  29. ';
  30. }
  31. } else {
  32. echo "ERROR: There was a problem with the query $sql.";
  33. }
  34. } else {
  35. }
  36. } else {
  37. echo 'There should be no direct access to this script!';
  38. }
  39. }
  40. ?>

人气教程排行