首頁 >後端開發 >php教程 >php mysql中utf8編碼漢字轉換成拼音

php mysql中utf8編碼漢字轉換成拼音

WBOY
WBOY原創
2016-07-25 08:53:431229瀏覽
  1. require_once('pinyin_table.php');

  2. function get_pinystring_array($🎜>function get_pinystring_array($🎜 >{
  3. global $pinyin_table;
  4. $flow = array();
  5. for ($i=0;$i {
  6. if ( ord($string[$i]) >= 0x81 且ord($string[$i]) {
  7. $h = ord($string[$i]);
  8. if (isset($string[$i 1]))
  9. {
  10. $i ;
  11. $l = ord($string[$i]);
  12. if (isset($ pinyin_table[$h][$l]))
  13. {
  14. array_push($flow,$pinyin_table[$h][$l]);
  15. }
  16. else
  17. {
  18. array_push($flow,$h);
  19. array_push($flow,$l);
  20. }
  21. }
  22. else
  23. {
  24. array_push($flow,ord($string[ $i]));
  25. }
  26. }
  27. else
  28. {
  29. array_push($flow,ord($string[$i]));
  30. }
  31. }
  32. //print_r($flow);
  33. $pinyin = array();
  34. $pinyin[0] = '';
  35. for ($i=0;$i< ;sizeof($flow);$i )
  36. {
  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. 如果($curbin {
  78. $tostr .= substr($name,$i,1);
  79. }elseif($curbin $str = substr( $名稱,$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($);
  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. ?>
  41. ?>
  42. ?>
  43. ?>
?>
?>?>?> /p>
複製程式碼


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn