
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP – Get the string length using mb_strlen()
In PHP, multibyte string length (mb_strlen) function is used to get the total string length of a specified string. This function is supported in PHP 4.6.0 or higher versions.
Syntax
int mb_strlen(str $string, str $encoding)
Parameters
mb_strlen() accepts two parameters: $string and $encoding.
$string− It is used to check the string length
$encoding− This parameter is used for character encoding. If it is omitted or null, then the internal character encoding value will be used.
Return Values
mb_strlen() returns the number of characters present in the given string. One is counted as a multi-byte character.
Errors/Exceptions
If the encoding is not known, then it will generate the level E_warning.
Example
<?php // It will return total number of character $result = mb_strlen("Hello World","UTF-8"); echo "Total number of characters: ", $result; ?>
Output
Total number of characters: 11
Note: that it counts the space too in the given string.
- Related Articles
- How to get the length of longest string in a PHP array
- PHP – How to get the selected part of a string using mb_substr()?
- Java string length without using length() method.
- Generating Random String Using PHP
- PHP program to find the length of the last word in the string
- How to get the length of a string in Python?
- How to get the length of a string in JavaScript?
- How to get the Length of a String in Java?
- How do I get the average string length in MySQL?
- PHP – How to get the substitution character using mb_substitute_character()?
- How to calculate the length of the string using C#?
- How to get the length of a string in bytes in JavaScript?
- PHP – Parse the GET, POST, and COOKIE data using mb_parse_str()
- How to get parameters from a URL string in PHP?
- Split string into sentences using regex in PHP

Advertisements