每天学习一点点,成功增加一大步

PHP 字符处理功能函数(收集中)

PHP zhanghui 444℃

每天学习一点点,收录一点点,收集一些常用(通用)的功能函数,有助项目的快速开发,以下收录的功能函数也是我在项目中实践过的没有问题的。列一下收录过的目录

  • 字符统计(包括中文、英文、数字和特殊符号)
  • 长字符中加换行符
  • 中文字符截取(不产生乱码)
  • 隐藏字符串中间部分字符
// 字符统计(包括中文、英文、数字和特殊符号)
function strHandler($str)
{
    $str_num = mb_strlen($str, $encode = 'UTF-8');
    $cn = 0;
    $en = 0;
    $num = 0;
    $other = 0;
    for ($i = 0; $i < $str_num; $i++) {
//            echo "\r\n" . mb_substr($str, $i, 1, $encode);
        if (ord(mb_substr($str, $i, 1, $encode)) > 0xa0) {
            $cn++;
        }
        if (preg_match("/^[a-zA-Z_\#]*$/", mb_substr($str, $i, 1, $encode))) {
            $en += 1;
        }
        if (preg_match("/^\d*$/", mb_substr($str, $i, 1, $encode))) {
            $num += 1;
        }
        if (preg_match("/[\'.,:;*?~\-`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/", mb_substr($str, $i, 1, $encode))) {
            $other += 1;
        }
    }
    echo "\r\n这个字符串的长度是:" . $str_num;
    echo "\r\n有" . $cn . "个汉字 ";
    echo "\r\n有" . $en . "个英文 ";
    echo "\r\n有" . $num . "个数字 ";
}
// 长字符中加换行符
function strChangeLineHandler($str, $len)
{
    $str_num = mb_strlen($str, $encode = 'UTF-8');
    $start = 0;
    $result = '';
    for ($i = 0; $i < $str_num; $i++) {
        $result .= mb_substr($str, $i, 1, $encode);
        if ($len == $start) {
            $start = 0;
            $result .= "\n";
        } else {
            $start += 1;
        }
    }
    return $result;
}
/**
 * 中文字符截取(不产生乱码)
 * 字符截取 要 UTF8 编码
 * @param $string 被处理的字符串
 * @param int $length 要截取的长度
 * @param string $dot
 * @return mixed|string
 */
function str_cut($string, $length = 100, $dot = '...') {
    $strlen = strlen($string);
    if ($strlen <= $length)
        return $string;
    $string = str_replace(array(' ', ' ', '&', '"', ''', '“', '”', '—', '<', '>', '·', '…'), array('∵', ' ', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), $string);
    $strcut = '';
    $length = intval($length - strlen($dot) - $length / 3);
    $n = $tn = $noc = 0;
    while ($n < strlen($string)) {
        $t = ord($string[$n]);
        if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
            $tn = 1;
            $n++;
            $noc++;
        } elseif (194 <= $t && $t <= 223) {
            $tn = 2;
            $n += 2;
            $noc += 2;
        } elseif (224 <= $t && $t <= 239) {
            $tn = 3;
            $n += 3;
            $noc += 2;
        } elseif (240 <= $t && $t <= 247) {
            $tn = 4;
            $n += 4;
            $noc += 2;
        } elseif (248 <= $t && $t <= 251) {
            $tn = 5;
            $n += 5;
            $noc += 2;
        } elseif ($t == 252 || $t == 253) {
            $tn = 6;
            $n += 6;
            $noc += 2;
        } else {
            $n++;
        }
        if ($noc >= $length) {
            break;
        }
    }
    if ($noc > $length) {
        $n -= $tn;
    }
    $strcut = substr($string, 0, $n);
    $strcut = str_replace(array('∵', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), array(' ', '&', '"', ''', '“', '”', '—', '<', '>', '·', '…'), $strcut);
    return $strcut . $dot;
}
/**
     * 隐藏字符串中间部分字符
     * @param $str
     * @param string $replacement
     * @param int $start
     * @param int $length
     * @return string
     */
    public static function SubstrHideReplace($str, $replacement = '*', $start = 1, $length = 3)
    {
        $len = mb_strlen($str,'utf-8');
        if ($len > intval($start+$length)) {
            $str1 = mb_substr($str,0,$start,'utf-8');
            $str2 = mb_substr($str,intval($start+$length),NULL,'utf-8');
        } else {
            $str1 = mb_substr($str,0,1,'utf-8');
            $str2 = mb_substr($str,$len-1,1,'utf-8');
            $length = $len - 2;
        }
        $new_str = $str1;
        for ($i = 0; $i < $length; $i++) {
            $new_str .= $replacement;
        }
        $new_str .= $str2;
        return $new_str;
    }

转载请注明:隨習筆記 » PHP 字符处理功能函数(收集中)

喜欢 (0)