欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php批量获取首字母(汉字、数字、英文)的代码

程序员文章站 2024-01-15 13:05:46
...
  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)echo $letter=iconv_substr($title,0,1,'utf-8');
  16. }else if(ord($title)>=65 and ord($title)echo $letter=iconv_substr($title,0,1,'utf-8');
  17. }else if(ord($title)>=97 and ord($title)echo $letter=iconv_substr($title,0,1,'utf-8');
  18. }
  19. }
  20. function getfirstchar($s0){

  21. $s=iconv("UTF-8","gb2312", $s0);
  22. $asc=ord($s{0})*256+ord($s伊雪)-65536;
  23. if($asc>=-20319 and $ascif($asc>=-20283 and $ascif($asc>=-19775 and $ascif($asc>=-19218 and $ascif($asc>=-18710 and $ascif($asc>=-18526 and $ascif($asc>=-18239 and $ascif($asc>=-17922 and $ascif($asc>=-17417 and $ascif($asc>=-16474 and $ascif($asc>=-16212 and $ascif($asc>=-15640 and $ascif($asc>=-15165 and $ascif($asc>=-14922 and $ascif($asc>=-14914 and $ascif($asc>=-14630 and $ascif($asc>=-14149 and $ascif($asc>=-14090 and $ascif($asc>=-13318 and $ascif($asc>=-12838 and $ascif($asc>=-12556 and $ascif($asc>=-11847 and $ascif($asc>=-11055 and $ascreturn false;
  24. }
  25. ?>
复制代码