php批量取得首字母(漢字、數字、英文)的程式碼

WBOY
發布: 2016-07-25 09:03:40
原創
786 人瀏覽過
  1. $mysql_server_name='127.0.0.1'; //改成自己的mysql数据库服务器

  2. $mysql_username='用户'; //改成自己的mysql数据库用户名
  3. $mysql_password='密码'; //改成自己的mysql数据库密码
  4. $mysql_database='数据库'; //改成自己的mysql数据库名
  5. mysql_connect('127.0.0.1', $mysql_username,$mysql_password) or die('database not access');
  6. mysql_select_db($mysql_database);
  7. mysql_query("SET NAMES 'utf8'");
  8. $equery2 = " select title from 表1 ";
  9. $result2=mysql_query($equery2);

  10. while ($row = mysql_fetch_array($result2, MYSQL_BOTH))

  11. {
  12. $title=$row["title"];
  13. if (ord($title)>128) { //汉字开头
  14. echo $letter=getfirstchar($title);
  15. }else if(ord($title)>=48 and ord($title)<=57){ //数字开头
  16. echo $letter=iconv_substr($title,0,1,'utf-8');
  17. }else if(ord($title)>=65 and ord($title)<=90){ //大写英文开头
  18. echo $letter=iconv_substr($title,0,1,'utf-8');
  19. }else if(ord($title)>=97 and ord($title)<=122){ //小写英文开头
  20. echo $letter=iconv_substr($title,0,1,'utf-8');
  21. }
  22. }

  23. function getfirstchar($s0){

  24. $s=iconv("UTF-8","gb2312", $s0);
  25. $asc=ord($s{0})*256+ord($s伊雪)-65536;
  26. if($asc>=-20319 and $asc<=-20284)return "A";
  27. if($asc>=-20283 and $asc<=-19776)return "B";
  28. if($asc>=-19775 and $asc<=-19219)return "C";
  29. if($asc>=-19218 and $asc<=-18711)return "D";
  30. if($asc>=-18710 and $asc<=-18527)return "E";
  31. if($asc>=-18526 and $asc<=-18240)return "F";
  32. if($asc>=-18239 and $asc<=-17923)return "G";
  33. if($asc>=-17922 and $asc<=-17418)return "H";
  34. if($asc>=-17417 and $asc<=-16475)return "J";
  35. if($asc>=-16474 and $asc<=-16213)return "K";
  36. if($asc>=-16212 and $asc<=-15641)return "L";
  37. if($asc>=-15640 and $asc<=-15166)return "M";
  38. if($asc>=-15165 and $asc<=-14923)return "N";
  39. if($asc>=-14922 and $asc<=-14915)return "O";
  40. if($asc>=-14914 and $asc<=-14631)return "P";
  41. if($asc>=-14630 and $asc<=-14150)return "Q";
  42. if($asc>=-14149 and $asc<=-14091)return "R";
  43. if($asc>=-14090 and $asc<=-13319)return "S";
  44. if($asc>=-13318 and $asc<=-12839)return "T";
  45. if($asc>=-12838 and $asc<=-12557)return "W";
  46. if($asc>=-12556 and $asc<=-11848)return "X";
  47. if($asc>=-11847 and $asc<=-11056)return "Y";
  48. if($asc>=-11055 and $asc<=-10247)return "Z";
  49. return false;
  50. }
  51. ?>

复制代码


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板