如何获取第一个字符??? 比如<?php echo $fields['uname']; ?>获取到的是“张麻子”如何获取第一个字,也就是只显示“张”一个字出来??? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果都是中文substr(“张麻子”,0,3) 如果中文是gbk编码就不对了,能用mb_系列的就用,自己处理麻烦,要么正则,要么便利串,判断首字符编码范围后截取。mb_substr("张麻子",0,1,'utf-8');//第4个参数指定成正确的编码。 在一个判断怎么写呢?比如<?php echo $fields['uname']; ?>是女,就显示:女士,男显示:先生 <?php echo ($fields['uname']=="女")?"女士":"先生";?> <?function getinitial($str){ $asc=ord(substr($str,0,1)); if ($asc<160) //非中文 { if ($asc>=48 && $asc<=57){ return '1'; //数字 }elseif ($asc>=65 && $asc<=90){ return chr($asc); // A--Z }elseif ($asc>=97 && $asc<=122){ return chr($asc-32); // a--z }else{ return '~'; //其他 } } else //中文 { $asc=$asc*1000+ord(substr($str,1,1)); //获取拼音首字母A--Z if ($asc>=176161 && $asc<176197){ return 'A'; }elseif ($asc>=176197 && $asc<178193){ return 'B'; }elseif ($asc>=178193 && $asc<180238){ return 'C'; }elseif ($asc>=180238 && $asc<182234){ return 'D'; }elseif ($asc>=182234 && $asc<183162){ return 'E'; }elseif ($asc>=183162 && $asc<184193){ return 'F'; }elseif ($asc>=184193 && $asc<185254){ return 'G'; }elseif ($asc>=185254 && $asc<187247){ return 'H'; }elseif ($asc>=187247 && $asc<191166){ return 'J'; }elseif ($asc>=191166 && $asc<192172){ return 'K'; }elseif ($asc>=192172 && $asc<194232){ return 'L'; }elseif ($asc>=194232 && $asc<196195){ return 'M'; }elseif ($asc>=196195 && $asc<197182){ return 'N'; }elseif ($asc>=197182 && $asc<197190){ return 'O'; }elseif ($asc>=197190 && $asc<198218){ return 'P'; }elseif ($asc>=198218 && $asc<200187){ return 'Q'; }elseif ($asc>=200187 && $asc<200246){ return 'R'; }elseif ($asc>=200246 && $asc<203250){ return 'S'; }elseif ($asc>=203250 && $asc<205218){ return 'T'; }elseif ($asc>=205218 && $asc<206244){ return 'W'; }elseif ($asc>=206244 && $asc<209185){ return 'X'; }elseif ($asc>=209185 && $asc<212209){ return 'Y'; }elseif ($asc>=212209){ return 'Z'; }else{ return '~'; } }}echo(getinitial("朝"));?>-------------------------------------------------------------------------------http://www.jiemengwu.com/ 解梦屋 http://www.phpzy.com/php/ 绿色php资源http://www.shopfw.com网店服务 echo cutstr($fields['uname'],1); /** * ---------------------------------------------------------- * 字符串截取,支持中文和其他编码 * ---------------------------------------------------------- * @param string $str 需要转换的字符串 * @param string $start 开始位置 * @param string $length 截取长度 * @param string $charset 编码格式 * @param string $suffix 截断显示字符 * @return string * ---------------------------------------------------------- */function cutstr($str, $length, $suffix='', $start=0, $charset="utf-8") { if(function_exists("mb_substr")) return mb_substr($str, $start, $length, $charset).$suffixStr; elseif(function_exists('iconv_substr')) { return iconv_substr($str,$start,$length,$charset).$suffixStr; } $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"; $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"; $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"; $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"; preg_match_all($re[$charset], $str, $match); $slice = join("",array_slice($match[0], $start, $length)); return $slice.$suffixStr;} <?php echo strpos($fields['uname'], '女')===0 ? '女士' : '男士'; ?> 简单的登录,我却一直在报错! 请教xuzuning,一个正则问题 求页面打开后自动打开迅雷下载和点击后迅雷下载 php move_uploaded_file 上传文件 问题 请问!WYSIWYG编辑器问题!! php读excel文件,不支持单元格之间的运算。 php初学者的PHP has encountered an Access Violation at 019273CD 问题 讨论一个问题,如何高效的学好php... 哪有PHP+MYSQL免费空间呀?(散分啦) 如何定義串類型 php5.3不支持$_GET[page]这样的函数了吗? 关于POST的问题 求大虾帮忙
如果中文是gbk编码就不对了,能用mb_系列的就用,自己处理麻烦,要么正则,要么便利串,判断首字符编码范围后截取。
mb_substr("张麻子",0,1,'utf-8');//第4个参数指定成正确的编码。
function getinitial($str)
{
$asc=ord(substr($str,0,1));
if ($asc<160) //非中文
{
if ($asc>=48 && $asc<=57){
return '1'; //数字
}elseif ($asc>=65 && $asc<=90){
return chr($asc); // A--Z
}elseif ($asc>=97 && $asc<=122){
return chr($asc-32); // a--z
}else{
return '~'; //其他
}
}
else //中文
{
$asc=$asc*1000+ord(substr($str,1,1));
//获取拼音首字母A--Z
if ($asc>=176161 && $asc<176197){
return 'A';
}elseif ($asc>=176197 && $asc<178193){
return 'B';
}elseif ($asc>=178193 && $asc<180238){
return 'C';
}elseif ($asc>=180238 && $asc<182234){
return 'D';
}elseif ($asc>=182234 && $asc<183162){
return 'E';
}elseif ($asc>=183162 && $asc<184193){
return 'F';
}elseif ($asc>=184193 && $asc<185254){
return 'G';
}elseif ($asc>=185254 && $asc<187247){
return 'H';
}elseif ($asc>=187247 && $asc<191166){
return 'J';
}elseif ($asc>=191166 && $asc<192172){
return 'K';
}elseif ($asc>=192172 && $asc<194232){
return 'L';
}elseif ($asc>=194232 && $asc<196195){
return 'M';
}elseif ($asc>=196195 && $asc<197182){
return 'N';
}elseif ($asc>=197182 && $asc<197190){
return 'O';
}elseif ($asc>=197190 && $asc<198218){
return 'P';
}elseif ($asc>=198218 && $asc<200187){
return 'Q';
}elseif ($asc>=200187 && $asc<200246){
return 'R';
}elseif ($asc>=200246 && $asc<203250){
return 'S';
}elseif ($asc>=203250 && $asc<205218){
return 'T';
}elseif ($asc>=205218 && $asc<206244){
return 'W';
}elseif ($asc>=206244 && $asc<209185){
return 'X';
}elseif ($asc>=209185 && $asc<212209){
return 'Y';
}elseif ($asc>=212209){
return 'Z';
}else{
return '~';
}
}
}
echo(getinitial("朝"));
?>
-------------------------------------------------------------------------------
http://www.jiemengwu.com/ 解梦屋 http://www.phpzy.com/php/ 绿色php资源http://www.shopfw.com网店服务
/**
* ----------------------------------------------------------
* 字符串截取,支持中文和其他编码
* ----------------------------------------------------------
* @param string $str 需要转换的字符串
* @param string $start 开始位置
* @param string $length 截取长度
* @param string $charset 编码格式
* @param string $suffix 截断显示字符
* @return string
* ----------------------------------------------------------
*/
function cutstr($str, $length, $suffix='', $start=0, $charset="utf-8") {
if(function_exists("mb_substr"))
return mb_substr($str, $start, $length, $charset).$suffixStr;
elseif(function_exists('iconv_substr')) {
return iconv_substr($str,$start,$length,$charset).$suffixStr;
}
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
$slice = join("",array_slice($match[0], $start, $length));
return $slice.$suffixStr;
}
<?php echo strpos($fields['uname'], '女')===0 ? '女士' : '男士'; ?>