Home  >  Article  >  Backend Development  >  Convert utf8 encoded Chinese characters to pinyin in php mysql

Convert utf8 encoded Chinese characters to pinyin in php mysql

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

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

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

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

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

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

复制代码

表中加入一个字段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. ?>

复制代码


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