Php code for batch obtaining the first letter (Chinese characters, numbers, and English letters)

Source: Internet
Author: User
Php code for batch obtaining the first letter (Chinese characters, numbers, and English letters)

  1. $ Mysql_server_name = '192. 0.0.1 '; // change it to your mysql database server.

  2. $ Mysql_username = 'user'; // change the username of your mysql database.
  3. $ Mysql_password = 'password'; // change the password of your mysql database.
  4. $ Mysql_database = 'database'; // change the name of your mysql database.
  5. Mysql_connect ('2014. 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 Table 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) {// starts with a Chinese character
  14. Echo $ letter = getfirstchar ($ title );
  15. } Else if (ord ($ title)> = 48 and ord ($ title) <= 57) {// start with a number
  16. Echo $ letter = iconv_substr ($ title, 0, 1, 'utf-8 ');
  17. } Else if (ord ($ title)> = 65 and ord ($ title) <= 90) {// starts with an uppercase letter
  18. Echo $ letter = iconv_substr ($ title, 0, 1, 'utf-8 ');
  19. } Else if (ord ($ title)> = 97 and ord ($ title) <= 122) {// start with lowercase English
  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 Yixue)-65536;
  26. If ($ asc >=- 20319 and $ asc <=-20284) return "";
  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. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.