Home  >  Article  >  Backend Development  >  Tips on using strlen() function in php

Tips on using strlen() function in php

autoload
autoloadOriginal
2021-04-29 10:47:462305browse

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   )
  • $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>";

?>
输出:  10
        12

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
?>
输出:6 2

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!

Statement:
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