Maison  >  Article  >  développement back-end  >  PHP 计算字符串长度

PHP 计算字符串长度

巴扎黑
巴扎黑original
2016-11-22 10:36:241278parcourir

/**
 * 计算字符串的长度(汉字按照两个字符计算)
 *
 * @param   string      $str        字符串
 *
 * @return  int
 */
function myStrLen($str){
    $length = strlen(preg_replace('/[\x00-\x7F]/', '', $str));

    if ($length){
        return strlen($str) - $length + intval($length / 3) * 2;
    }
    else{
        return strlen($str);
    }
}

/**
 * 计算字符串的长度(汉字按照一个字符计算)
 *
 * @param   string      $str        字符串
 *
 * @return  int
 */
function cnForOneBetLen($str){
    $length = strlen(preg_replace('/[\x00-\x7F]/', '', $str));

    if ($length){
        return strlen($str) - $length + intval($length / 3) * 1;
    }
    else{
        return strlen($str);
    }
}


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn