What is the function in php to find the length of a string?

青灯夜游
Release: 2023-03-16 13:06:01
Original
5875 people have browsed it

php has two functions to find the length of a string: 1. The strlen() function can calculate the length of an English string, and the syntax is "strlen($string)"; 2. The mb_strlen() function can By setting the character encoding, calculate the length of an English string, a Chinese string, or a mixed Chinese-English string. The syntax is "mb_strlen($string,"Character encoding")".

What is the function in php to find the length of a string?

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer

Commonly used in PHP to calculate the length of a string There are two functions, namely strlen() and mb_strlen() functions. When processing all-English strings, the effects of these two functions are the same. However, when processing mixed Chinese and English strings or pure Chinese strings, these two functions will have some differences:

  • # The ##strlen() function can calculate the length of English strings, but for mixed Chinese and English or pure Chinese strings, the length cannot be accurately calculated.

  • mb_strlen() function can calculate the length of English strings, Chinese strings or mixed Chinese and English strings.

1. strlen() function finds the length of a string

strlen() function can return the length of a given string, its syntax format As follows:

strlen($string)
Copy after login

  • The parameter $string is the string whose length needs to be calculated. Returns 0 if $string is empty.

Note: In the strlen() function, numbers, English, decimal point, underline and space occupy one character; while a GB2312 encoded Chinese character occupies two characters. A UTF-8 encoded Chinese character is three characters long.

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$str = "hello world!";
echo &#39;字符串 “&#39;.$str.&#39;” 的长度为:&#39;.strlen($str).&#39;<br>&#39;;
?>
Copy after login

$str string contains 10 English letters, 1 space and 1 English exclamation mark, a total of 12 characters, so the output length is 12.

What is the function in php to find the length of a string?

But the strlen() function is not suitable for calculating the length of Chinese strings, because in this function one GB2312 encoded Chinese character occupies two characters in length, one UTF-8 The encoded Chinese characters are three characters long.

<?php
header("Content-type:text/html;charset=utf-8");
$str = "hello 你好!";
echo &#39;字符串 “&#39;.$str.&#39;” 的长度为:&#39;.strlen($str).&#39;<br>&#39;;
?>
Copy after login

What is the function in php to find the length of a string?

2. mb_strlen() function finds the length of a string

mb_strlen() function can also return the length of a string. The syntax format is as follows:

mb_strlen($str [, $encoding = mb_internal_encoding()])
Copy after login

  • $str is the string whose length needs to be calculated

  • $encoding is an optional parameter, which is the character encoding. If If omitted, the internal character encoding is used.

The return value of the mb_strlen() function is the number of characters contained in the string $str with $encoding encoding. If $encoding is invalid, false is returned.

Note: Unlike the strlen() function, in the mb_strlen() function, whether it is Chinese characters, English, numbers, decimal points, underscores and spaces, they only occupy one character in length.

[Example] Use the mb_strlen() function to get the string length.

<?php
header("Content-type:text/html;charset=utf-8");
$str = "hello 你好!";
echo &#39;字符串 “&#39;.$str.&#39;” 的长度为:&#39;.mb_strlen($str,"utf-8").&#39;<br>&#39;;

$str = "欢迎来到PHP中文网!";
echo &#39;<br>字符串 “&#39;.$str.&#39;” 的长度为:&#39;.mb_strlen($str,&#39;utf-8&#39;).&#39;<br>&#39;;
?>
Copy after login

What is the function in php to find the length of a string?

It can be seen that we can well handle the length problem of Chinese strings by setting the corresponding

$encoding parameters.

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of What is the function in php to find the length of a string?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template