通常情况下要想掌握一个字符串变量的长度[一般掌握其字数],自然想到 strlen

|——

 
$str = 'string';
echo strlen($str); //6

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

|——

 
$str = "方言";
echo strlen($str);//6

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

问题来了,明明是两个字的怎么会得到6的结果:在PHP自带的函数中,strlen及mb_strlen都是通过计算字符串所占字节数来计算长度的,在不同的编码情况下,中文所占的字节数是不同的。在GBK/GB2312下,中文字符占2个字节,而在UTF-8下,中文字符占3个字节。

[我们不需要掌握字节数,没有意义,只要掌握字数就行]

|——

$str = '你好,世界!';
echo strlen($str);    // GBK或GB2312下输出12,UTF-8下输出18

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Wordpress 的代码可以借鉴

|——

$str = 'Hello,世界!';
preg_match_all('/./us', $str, $match);
echo count($match[0]);    // 输出9

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

思路是用正则将字符串切割成单个字符,直接获取匹配到的结果,得到结果。

但以上代码在UTF-8编码下并不能处理GBK/GB2312的中文字符串,因为GBK/GB2312的中文字符会被识别为两个字符而计算出来的中文字符数量会翻倍。

|——

$tmp = @iconv('gbk', 'utf-8', $str);
if(!empty($tmp)){
    $str = $tmp;
}
preg_match_all('/./us', $str, $match);
echo count($match[0]);

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

这样可以做到utf-8 和gbk环境下的兼容,但是一般日常我们都是掌握编码环境的[utf-8];

这样我们获取字符变量的字数就用:

|——

int iconv_strlen ( string $str [, string $charset = ini_get("iconv.internal_encoding") ] );

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

 

参考:

http://www.clanfei.com/2012/12/1671.html

PHP 获取中英文混合字符串长度的更多相关文章

  1. PHP获取中英文混合字符串长度及截取

    1.字符串长度 PHP获取中英文混合字符串长度的实现代码如下,1中文=1位,2英文=1位,可自行修改 /** * PHP获取字符串中英文混合长度 * @param $str string 字符串 *  ...

  2. PHP中获取中英文混合字符串长度[主要是指个数,而不是字符串长度](转)

    今晚在写框架的表单验证类时,需要判断某个字符串长度是否在指定区间内,很自然地,想到了PHP中的strlen函数. $str = 'Hello world!'; echo strlen($str);   ...

  3. CSS截取中英文混合字符串长度

    <!doctype html> <html> <head> <meta http-equiv="content-type" content ...

  4. PHP针对中英文混合字符串长度判断及截取方法

    PHP自带的函数如strlen().mb_strlen()都是通过计算字符串所占字节数来统计字符串长度的,一个英文字符占1字节.例: $enStr = 'Hello,China!'; echo str ...

  5. php 中英文混合字符串长度计算

    (strlen($string) + mb_strlen($string,'UTF8')) / 2;tw 这样计算的

  6. js 计算中英文混合字符串长度

    转载请注明来源:https://www.cnblogs.com/hookjc/ function isChinese(str) {    var lst = /[u00-uFF]/;          ...

  7. C#与JS实现 获取指定字节长度 中英文混合字符串 的方法

    平时在作数据库插入操作时,如果用 INSERT 语句向一个varchar型字段插入内容时,有时会因为插入的内容长度超出规定的长度而报错. 尤其是插入中英文混合字符串时,SQL Server中一般中文要 ...

  8. 用C#截取指定长度的中英文混合字符串

    很早以前写过一篇文章(用C#截取指定长度的中英文混合字符串),但是对性能没有测试,有人说我写的这个方法性能有问题,后来想,可能真会有BT之需求要求传入一个几万K甚至几M体积的字符串进来,那将会影响正则 ...

  9. c#的中英文混合字符串截取指定长度,startidx从0开始

    //c#的中英文混合字符串截取指定长度,startidx从0开始 by gisoracle@126.com public string getStrLenB(string str, int start ...

随机推荐

  1. LeetCode——Find All Duplicates in an Array

    Question Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice an ...

  2. Javascript实用技巧

    1. 给参数赋默认值 //通常写法 function dateRender(format){ if(format){ format = 'Y-m-d'; } // code } //强推 functi ...

  3. consul 小結

    Consul Config 使用Git做版本控制的实现 https://segmentfault.com/a/1190000013807641 服务发现 - consul 的介绍.部署和使用 http ...

  4. emmet常用指令组合

    emmet的应用   1.生成html(需要先将文件命名为.html后缀) !+tab,html:5+tab   2.生成meta utf meta:utf+tab   3.生成meta viewpo ...

  5. Hystrix的正确理解方式

    hystrix-logo-tagline-640.png 什么是熔断器 熔断器,原本是电路中在电器发生短路时的防止电路过载的开关装置,它切断发生短路的电路,从而防止因电路过载导致的发热起火等灾难的发生 ...

  6. CountDownLatch详解

    功能描述 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 常见用法 多个人等一个信号后继续执行操作.例如5个运动员,等一个发令员的枪响. 一个人等多个人的信号. ...

  7. 为什么需要超出48K的音频采样率,以及PCM到DSD的演进

    网上很多观点说,根据采样定理,48K的音频采样率即可无损的表示音频模拟信号(人耳最多可以听到20K的音频),为何还需要96K, 192K等更高的采样率呢?最先我也有这样的疑问,毕竟采样定理是经过数学家 ...

  8. yii2手动添加图片处理插件Imagine

    1.首先从官网下载yii2-imagine的拓展 下载地址:https://github.com/yiisoft/yii2-imagine 下载包名称:yii2-imagine-master 2.然后 ...

  9. Python 常用 PEP8 编码规范

    Python 常用 PEP8 编码规范 代码布局 缩进 每级缩进用4个空格. 括号中使用垂直隐式缩进或使用悬挂缩进. EXAMPLE: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  10. 验证email是否合法

    https://buluo.qq.com/p/detail.html?bid=339910&pid=6675390-1514450689&from=grp_sub_obj 场景1:验证 ...