熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> PHP編程 >> 正文

php批量獲取首字母(漢字、數字、英文)

2022-06-13   來源: PHP編程 

  php批量獲取首字母(漢字數字英文)

  $mysql_server_name=; //改成自己的mysql數據庫服務器

  $mysql_username=用戶; //改成自己的mysql數據庫用戶名

  $mysql_password=密碼; //改成自己的mysql數據庫密碼

  $mysql_database=數據庫; //改成自己的mysql數據庫名

  mysql_connect( $mysql_username$mysql_password) or die(database not access);

  mysql_select_db($mysql_database);

  mysql_query("SET NAMES utf");

  $equery = " select title from 表 ";

  $result=mysql_query($equery);

  while ($row = mysql_fetch_array($result MYSQL_BOTH))

  {

  $title=$row["title"];

  if (ord($title)>) { //漢字開頭

  echo $letter=getfirstchar($title);

  }else if(ord($title)>= and ord($title)<=){ //數字開頭

  echo $letter=iconv_substr($titleutf);

  }else if(ord($title)>= and ord($title)<=){ //大寫英文開頭

  echo $letter=iconv_substr($titleutf);

  }else if(ord($title)>= and ord($title)<=){ //小寫英文開頭

  echo $letter=iconv_substr($titleutf);

  }

  }

  function getfirstchar($s){

  $s=iconv("UTF""gb" $s);

  $asc=ord($s{})*+ord($s{});

  if($asc>= and $asc<=)return "A";

  if($asc>= and $asc<=)return "B"; if($asc>= and $asc<=)return "C";

  if($asc>= and $asc<=)return "D";

  if($asc>= and $asc<=)return "E";

  if($asc>= and $asc<=)return "F";

  if($asc>= and $asc<=)return "G";

  if($asc>= and $asc<=)return "H";

  if($asc>= and $asc<=)return "J";

  if($asc>= and $asc<=)return "K";

  if($asc>= and $asc<=)return "L";

  if($asc>= and $asc<=)return "M";

  if($asc>= and $asc<=)return "N";

  if($asc>= and $asc<=)return "O";

  if($asc>= and $asc<=)return "P";

  if($asc>= and $asc<=)return "Q";

  if($asc>= and $asc<=)return "R";

  if($asc>= and $asc<=)return "S";

  if($asc>= and $asc<=)return "T";

  if($asc>= and $asc<=)return "W";

  if($asc>= and $asc<=)return "X";

  if($asc>= and $asc<=)return "Y";

  if($asc>= and $asc<=)return "Z";

  return false;

  }


From:http://tw.wingwit.com/Article/program/PHP/201311/20913.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.