温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php中常用的字符串长度函数strlen()与mb_strlen()实例解释

发布时间:2020-06-11 14:05:06 来源:网络 阅读:379 作者:jinsanguo 栏目:建站服务器

int strlen ( string $string ) 

int strlen ( string $string )  获取给定字符串的[字节]长度 成功则返回字符串$string的长度,如果$string为空,则返回 0。

<?php
	$str1 = "abcdef";		//输出6
	$str2 = " ab cd ";		//输出7,注意,开头、结尾、中间的空格
	$str3 = "中国你好";		//输出12,但会变化,与系统所采用的字符编码方式有关
	$str4 = "中国,你好";	//输出15,但会变化,与系统所采用的字符编码方式有关
	echo '$str1的字节长度为:'.strlen($str1).'$str2的字节长度为:'.strlen($str2).'';
	echo '$str3的字节长度为:'.strlen($str3).'$str4的字节长度为:'.strlen($str4).''; 
?>

mb_strlen() — 获取字符串的长度
mixed mb_strlen ( string $str [, string $encoding = mb_internal_encoding() ] )
$str 要检查长度的字符串
$encoding,可指定字符编码,如省略则使用内部字符编码
返回值:返回具有encoding编码的字符串str包含的[字符数],多字节的字符被计为 1

<?php
	$str1 = "abcdef";		//输出6
	$str2 = " ab cd ";		//输出7		注意,开头、结尾、中间的空格
	$str3 = "中国你好";		//输出4
	$str4 = "中国,你好";	//输出5
	echo '$str1的字符长度为:'.mb_strlen($str1,"utf-8").'$str2的字符长度为:'.mb_strlen($str2,"utf-8").'';
	echo '$str3的字符长度为:'.mb_strlen($str3,"utf-8").'$str4的字符长度为:'.mb_strlen($str4,"utf-8").'';
?>


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI