Heim > Backend-Entwicklung > PHP-Tutorial > php mysql中utf8编码汉字转换成拼音

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

WBOY
Freigeben: 2016-07-25 08:53:43
Original
1211 Leute haben es durchsucht
  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]) {
  8. $h = ord($string[$i]);
  9. if (isset($string[$i+1]))
  10. {
  11. $i++;
  12. $l = ord($string[$i]);
  13. if (isset($pinyin_table[$h][$l]))
  14. {
  15. array_push($flow,$pinyin_table[$h][$l]);
  16. }
  17. else
  18. {
  19. array_push($flow,$h);
  20. array_push($flow,$l);
  21. }
  22. }
  23. else
  24. {
  25. array_push($flow,ord($string[$i]));
  26. }
  27. }
  28. else
  29. {
  30. array_push($flow,ord($string[$i]));
  31. }
  32. }
  33. //print_r($flow);
  34. $pinyin = array();
  35. $pinyin[0] = '';
  36. for ($i=0;$i {
  37. if (is_array($flow[$i]))
  38. {
  39. if (sizeof($flow[$i]) == 1)
  40. {
  41. foreach ($pinyin as $key => $value)
  42. {
  43. $pinyin[$key] .= $flow[$i][0];
  44. }
  45. }
  46. if (sizeof($flow[$i]) > 1)
  47. {
  48. $tmp1 = $pinyin;
  49. foreach ($pinyin as $key => $value)
  50. {
  51. $pinyin[$key] .= $flow[$i][0];
  52. }
  53. for ($j=1;$j {
  54. $tmp2 = $tmp1;
  55. for ($k=0;$k {
  56. $tmp2[$k] .= $flow[$i][$j];
  57. }
  58. array_splice($pinyin,sizeof($pinyin),0,$tmp2);
  59. }
  60. }
  61. }
  62. else
  63. {
  64. foreach ($pinyin as $key => $value)
  65. {
  66. $pinyin[$key] .= chr($flow[$i]);
  67. }
  68. }
  69. }
  70. return $pinyin;
  71. }
  72. function GetGB2312String($name)

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

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

  124. $result = mysql_query("set names utf8",$db);
  125. $result = mysql_query("select title,group_id from groups",$db);
  126. while ($myrow = mysql_fetch_array($result)) {

  127. $text1="$myrow[0]";
  128. $text= GetGB2312String($text1);//获得GB2312编码串
  129. $flow = get_pinyin_array($text);//获得拼音
  130. $result2 = mysql_query("update groups set titlepinyin='$flow[0]' where group_id = $myrow[1]",$db);
  131. printf("%s
    %s
    ",$text1,$flow[0]);
  132. }
  133. ?>
复制代码

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


Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage