PHP gets the first character of the Chinese string Phonetic alphabet

Source: Internet
Author: User
  1. $str = "This is a test program 1";
  2. echo Getfirstcharcode ($STR);
  3. function Getfirstcharcode ($STR) {
  4. $str = Iconv ("UTF-8", "gb2312", $str);
  5. $targetChar = ' * ';
  6. $i = 0;
  7. while ($i
  8. $tmp =bin2hex (substr ($str, $i, 1));
  9. if ($tmp >= ' B0 ') {//start of Kanji
  10. $t =getletter (Hexdec (Bin2Hex (substr ($str, $i, 2)));
  11. $targetChar = $t ==-1? ' * ': $t;
  12. printf ("%c", $t ==-1? ' * ': $t);
  13. Break
  14. $i +=2;
  15. }
  16. else{
  17. $targetChar =substr ($str, $i, 1);
  18. Break
  19. printf ("%s", substr ($str, $i, 1));
  20. $i + +;
  21. }
  22. }
  23. if (Is_numeric ($targetChar)) {
  24. return Chr ($targetChar);
  25. }
  26. else{
  27. return $targetChar;
  28. }
  29. }
  30. function Getletter ($num) {
  31. $limit =array (//gb2312 pinyin sort
  32. Array (45217,45252),//a
  33. Array (45253,45760),//b
  34. Array (45761,46317),//c
  35. Array (46318,46825),//d
  36. Array (46826,47009),//e
  37. Array (47010,47296),//f
  38. Array (47297,47613),//g
  39. Array (47614,48118),//h
  40. Array (0,0),//i
  41. Array (48119,49061),//j
  42. Array (49062,49323),//k
  43. Array (49324,49895),//l
  44. Array (49896,50370),//m
  45. Array (50371,50613),//n
  46. Array (50614,50621),//o
  47. Array (50622,50905),//p
  48. Array (50906,51386),//q
  49. Array (51387,51445),//r
  50. Array (51446,52217),//s
  51. Array (52218,52697),//t
  52. Array (0,0),//u
  53. Array (0,0),//v
  54. Array (52698,52979),//w
  55. Array (52980,53688),//x
  56. Array (53689,54480),//y
  57. Array (54481,55289),//z
  58. );
  59. $char _index=65;
  60. foreach ($limit as $k = = $v) {
  61. if ($num >= $v [0] && $num <= $v [1]) {
  62. $char _index+= $k;
  63. return $char _index;
  64. }
  65. }
  66. return-1;
  67. }
Copy Code
Php
  • 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.