PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP strlen() And mb_strlen(): Get String Length

In PHP, strlen() and mb_strlen() functions are used to get the length of a string. However, they handle multi-byte characters differently.

1. strlen():

The strlen() function returns the number of bytes in a string.

Here's an example of how to use it:

$str = "Hello, World!";
echo strlen($str); // Outputs: 13

In the above example, strlen() counts each character as one byte, so it returns 13.

However, strlen() doesn't work correctly with multi-byte characters, like those found in UTF-8 encoded strings. For example:

$str = "こんにちは";
echo strlen($str); // Outputs: 15

In the above example, strlen() returns 15 because it's counting bytes, not characters. Each of these Japanese characters is a multi-byte character in UTF-8.

2. mb_strlen():

The mb_strlen() function returns the number of characters in a string, and it's multi-byte safe.

Here's an example of how to use it:

$str = "Hello, World!";
echo mb_strlen($str); // Outputs: 13

In the above example, mb_strlen() also counts each character as one, so it returns 13.

However, unlike strlen(), mb_strlen() also works correctly with multi-byte characters:

$str = "こんにちは";
echo mb_strlen($str); // Outputs: 5

In the above example, mb_strlen() correctly returns 5, because there are five characters in the string.

So, if you're working with multi-byte character encodings like UTF-8, it's safer to use mb_strlen() to get the length of a string. If you're certain your strings will only contain single-byte characters, you can use strlen().

  1. Get string length in PHP with strlen():

    • The strlen() function is used to determine the length of a string in PHP.
    // File: string_length_strlen.php
    $string = "Hello, World!";
    $length = strlen($string);
    
    echo "String: $string\nLength: $length";
    // Output: String: Hello, World!
    //         Length: 13
    
  2. Multibyte string length in PHP with mb_strlen():

    • For multibyte character encodings like UTF-8, use mb_strlen() to accurately get the length.
    // File: multibyte_string_length_mb_strlen.php
    $string = "��ã����磡";
    $length = mb_strlen($string, 'UTF-8');
    
    echo "String: $string\nLength: $length";
    // Output: String: ��ã����磡
    //         Length: 6
    
  3. PHP strlen() vs mb_strlen() differences:

    • strlen() counts bytes, while mb_strlen() counts characters, making it suitable for multibyte encodings.
    // File: strlen_vs_mb_strlen.php
    $string = "��ã����磡";
    $lengthStrlen = strlen($string);
    $lengthMbStrlen = mb_strlen($string, 'UTF-8');
    
    echo "String: $string\nLength (strlen): $lengthStrlen\nLength (mb_strlen): $lengthMbStrlen";
    // Output: String: ��ã����磡
    //         Length (strlen): 18
    //         Length (mb_strlen): 6
    
  4. Handling UTF-8 characters with mb_strlen() in PHP:

    • Use mb_strlen() with the appropriate character encoding to handle UTF-8 characters correctly.
    // File: utf8_handling_mb_strlen.php
    $string = "����ˤ��ϡ����磡";
    $length = mb_strlen($string, 'UTF-8');
    
    echo "String: $string\nLength: $length";
    // Output: String: ����ˤ��ϡ����磡
    //         Length: 10
    
  5. Example code for strlen() and mb_strlen() in PHP:

    • Compare the results of strlen() and mb_strlen() for strings with multibyte characters.
    // File: example_strlen_mb_strlen.php
    $string = "Caf��";
    $lengthStrlen = strlen($string);
    $lengthMbStrlen = mb_strlen($string, 'UTF-8');
    
    echo "String: $string\nLength (strlen): $lengthStrlen\nLength (mb_strlen): $lengthMbStrlen";
    // Output: String: Caf��
    //         Length (strlen): 5
    //         Length (mb_strlen): 4
    
  6. Case-insensitive string length with mb_strlen() in PHP:

    • mb_strlen() can be used with the mb_strtolower() function for case-insensitive length.
    // File: case_insensitive_length_mb_strlen.php
    $string = "Caf��";
    $lowercaseString = mb_strtolower($string, 'UTF-8');
    $length = mb_strlen($lowercaseString);
    
    echo "String: $string\nLowercase: $lowercaseString\nLength: $length";
    // Output: String: Caf��
    //         Lowercase: caf��
    //         Length: 4
    
  7. Detecting character encoding before using mb_strlen() in PHP:

    • Detect the character encoding before using mb_strlen() to ensure accurate results.
    // File: detect_encoding_before_mb_strlen.php
    $string = "��ã����磡";
    $encoding = mb_detect_encoding($string, 'UTF-8, ISO-8859-1', true);
    $length = mb_strlen($string, $encoding);
    
    echo "String: $string\nEncoding: $encoding\nLength: $length";
    // Output: String: ��ã����磡
    //         Encoding: UTF-8
    //         Length: 6