2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > php 获取字符串长度 包含空格 php中常用的字符串长度函数strlen()与mb_strlen()实例解释...

php 获取字符串长度 包含空格 php中常用的字符串长度函数strlen()与mb_strlen()实例解释...

时间:2024-01-26 07:43:12

相关推荐

php 获取字符串长度 包含空格 php中常用的字符串长度函数strlen()与mb_strlen()实例解释...

int strlen ( string $string )

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

$str1="abcdef";//输出6

$str2="abcd";//输出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="abcd";//输出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").'';

?>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。