<?php
/**
* 计算字符串的长度(汉字按照两个字符计算)
*
* @param string $str 字符串
*
* @return int
*/
function str_len($str)
{
$length = strlen(preg_replace('/[x00-x7F]/', '', $str)); if ($length)
{
return strlen($str) - $length + intval($length / 3) * 2;
}
else
{
return strlen($str);
}
}

PHP计算字符串的长度的更多相关文章

  1. 应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1) String.prototype.len=function(){return this.replace(/[^\x00-\xff]/g,"aa").length;}

    应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1) String.prototype.len=function(){return this.replace(/[^\x00-\xff] ...

  2. JS计算字符串的长度

    最近项目上经常要用到计算字符串的长度的问题,有时需要按照byte进行计算长度,所以我就想在页面上用js实现,于是就到网上查了相关的资料,发现确实有很多的版本,这里给出两个比较好用的. //方法一:逐个 ...

  3. js混合计算字符串字节长度

    js混合计算字符串字节长度 function getBt(str) { ) { var char = str.match(/[^\x00-\xff]/ig); : char.length); } el ...

  4. 计算字符串的长度.len,RuneCountInString

    内置函数len(),可以返回字符串/数组/切片/map/channel的长度. unicode/utf8包 函数:RuneCountInString(输入一个字符串),返回int类型的字符串长度.由于 ...

  5. Java 按字节计算字符串的长度

       在<从后向前截取指定长度的字符串>中介绍了如何截取字符串,本文介绍如何统计字符串的字节长度. import org.slf4j.Logger; import org.slf4j.Lo ...

  6. JS计算字符串实际长度

    http://www.qttc.net/201207136.html // UTF8字符集实际长度计算 function getStrLeng(str){ var realLength = 0; va ...

  7. c语言利用指针计算字符串的长度

    可以用strlen函数,这里我们自己写一个. 注意:不能用scanf,scanf一遇到空格就认为输入结束.应该用gets(),遇到换行符或EOF结束.说明可以接受空格. #include<cst ...

  8. Problem C: 指针:自定义函数length,调用它计算字符串的长度

    #include<stdio.h> int length(char*s) { int i,count; while(*s!='\0') { *(s++); count++; } retur ...

  9. php计算字符串长度

    /** * 计算字符串的长度(非字节) * 先用正则将字符串分解为个体单元,然后再计算单元的个数即得出字符串的长度 * from wordpress * @param string $string * ...

随机推荐

  1. vi写完文件保存时才发现是readonly😂

    在MAC上编辑apache配置文件,老是忘记sudo…… readonly的文件保存时提示 add ! to override, 但这仅是对root来说的啊! 百毒了一下竟然还有解决方案!! :w ! ...

  2. 洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 解题报告

    P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题意: 给定一个长\(N\)的序列,求满足任意两个相邻元素之间的绝对值之差不超过\(K\)的这个序列的排列有多少个? 范围: ...

  3. Android 5.0 Lollipop SDK下载地址(PASS)

    Android 5.0 ARM EABI v7a System Image https://dl-ssl.google.com/android/repository/sys-img/google_ap ...

  4. bzoj3926: [Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机模板

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #d ...

  5. MVC中使用RadioButtonFor

    http://shw3588.blog.163.com/blog/static/6507576201321395845538/ 1 进行初始化 <%=Html.RadioButtonFor(mo ...

  6. Nginx反向代理websocket配置实例(官网)

    https://www.nginx.com/blog/websocket-nginx/ Blog Tech Rick Nelson of NGINX, Inc.   May 16, 2014   NG ...

  7. bzoj千题计划156:bzoj1571: [Usaco2009 Open]滑雪课Ski

    http://www.lydsy.com/JudgeOnline/problem.php?id=1571 DP不一定全部全状态转移 贪心的舍去一些不合法的反而更容易转移 在一定能力范围内,肯定滑雪所需 ...

  8. BFS搜索:POJ No 3669 Meteor Shower

    #include <iostream> #include <cstring> #include <queue> #include <cstdio> #i ...

  9. CSS3实战之content

    为元素添加内容 content属性术语内容生成和替换模块,该属性能够为指定元素添加内容. 取值如下 normal:默认值 string:文本内容 attr():插入元素的属性值 uri():插入一个外 ...

  10. django错误笔记——URL

    django提交表单提示"RuntimeError: You called this URL via POST, but the URL doesn’t end in a slash and ...