比如<?php echo $fields['uname']; ?>获取到的是“张麻子”如何获取第一个字,也就是只显示“张”一个字出来???

解决方案 »

  1.   

    如果都是中文substr(“张麻子”,0,3)
      

  2.   


    如果中文是gbk编码就不对了,能用mb_系列的就用,自己处理麻烦,要么正则,要么便利串,判断首字符编码范围后截取。
    mb_substr("张麻子",0,1,'utf-8');//第4个参数指定成正确的编码。
      

  3.   

    在一个判断怎么写呢?比如<?php echo $fields['uname']; ?>是女,就显示:女士,男显示:先生
      

  4.   

    <?php echo ($fields['uname']=="女")?"女士":"先生";?>
      

  5.   

    <?
    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网店服务
      

  6.   

    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;
    }
      

  7.   


    <?php echo strpos($fields['uname'], '女')===0 ? '女士' : '男士'; ?>