Tips on using strlen() function in php

autoload
Release: 2023-03-09 12:36:01
Original
2295 people have browsed it

String is a frequently used data type in php. We often need to determine the length of the string. In this case, we need to use the strlen() function. This article will take you with us. Take a look at this function. First, let’s take a look at the syntax of the strlen() function:

strlen    ( string $string   )
Copy after login
  • $string: The string whose length needs to be calculated.

  • Return value: If successful, the length of the string $string is returned; if string is empty, 0 is returned.

Usage examples:

1.$str is an English string

<?php
$str = &#39;fdgdfgsdfg&#39;;
echo strlen($str); 
echo "<br>";

$str = &#39;dfgdf ab cd &#39;;
echo strlen($str); 
echo "<br>";

?>
Copy after login
输出:  10
        12
Copy after login

2.$str is a Chinese string

We can find that it is very convenient for English, but it needs to be converted for Chinese characters, so we can use mb_strlen()

<?php
$str = "你好";
echo strlen($str); 
echo mb_strlen($str,&#39;UTF-8&#39;); // 2
?>
Copy after login
输出:6 2
Copy after login

Recommended: 2021 PHP interview questions summary (collection)》《php video Tutorial

The above is the detailed content of Tips on using strlen() function in php. 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