Php string length functions

PHP strlen

Summary: in this tutorial, you’ll learn how to use the PHP strlen() function to get the length of a string.

Introduction to the PHP strlen() function

The strlen() function returns the length of a specified string. Here’s the syntax of the strlen() function:

strlen ( string $string ) : intCode language: PHP (php)

The strlen() function has one parameter $string , which is the string to measure the length. The strlen() function returns the length of the $string in bytes or zero if the $string is empty.

It’s important to note that the strlen() function returns the number of bytes rather than the number of characters. If each character is 1 byte, the number of bytes is the same as the number of characters.

However, if you deal with the multibyte string, e.g., UTF-8, the number of bytes is higher than the number of characters.

To get the number of characters in a multibyte string, you should use the mb_strlen() function instead:

mb_strlen ( string $string , string|null $encoding = null ) : intCode language: PHP (php)

The mb_strlen() function has an additional $encoding that specifies the character encoding of the $string .

The mb_strlen() function returns the number of characters in the $string having character $encoding . The mb_strlen() returns one for each multibyte character.

PHP strlen() function examples

Let’s take some examples of using the strlen() function.

1) Simple strlen() function example

The following example uses the strlen() function to return the length of the string PHP:

 $str = 'PHP'; echo strlen($str); // 3Code language: PHP (php)

2) Using the strlen() with a multibyte string

The following multibyte string has five characters. However, its size is 15 bytes.

'こんにちは'Code language: PHP (php)

By the way, こんにちは is a greeting in Japanese. It means hello in English.

The strlen() function returns 15 bytes for the string ‘こんにちは’ :

 $message = 'こんにちは'; echo strlen($message); // 15 bytes Code language: PHP (php)

But the mb_strlen() function returns five characters for that string:

 $message = 'こんにちは'; echo mb_strlen($message); // 5 charactersCode language: PHP (php)

Summary

  • Use the PHP strlen() function to get the number of bytes of a string.
  • Use the PHP mb_strlen() function to get the number of characters in a string with a specific encoding.

Источник

strlen

Note:

strlen() returns the number of bytes rather than the number of characters in a string.

See Also

  • count() — Counts all elements in an array or in a Countable object
  • grapheme_strlen() — Get string length in grapheme units
  • iconv_strlen() — Returns the character count of string
  • mb_strlen() — Get string length

User Contributed Notes 7 notes

I want to share something seriously important for newbies or beginners of PHP who plays with strings of UTF8 encoded characters or the languages like: Arabic, Persian, Pashto, Dari, Chinese (simplified), Chinese (traditional), Japanese, Vietnamese, Urdu, Macedonian, Lithuanian, and etc.
As the manual says: «strlen() returns the number of bytes rather than the number of characters in a string.», so if you want to get the number of characters in a string of UTF8 so use mb_strlen() instead of strlen().

// the Arabic (Hello) string below is: 59 bytes and 32 characters
$utf8 = «السلام علیکم ورحمة الله وبرکاته!» ;

var_export ( strlen ( $utf8 ) ); // 59
echo «
» ;
var_export ( mb_strlen ( $utf8 , ‘utf8’ ) ); // 32
?>

Since PHP 8.0, passing null to strlen() is deprecated. To check for a blank string (not including ‘0’):

// PHP >= 8.0
if ( $text === null || $text === » )) echo ’empty’ ;
>

When checking for length to make sure a value will fit in a database field, be mindful of using the right function.

There are three possible situations:

1. Most likely case: the database column is UTF-8 with a length defined in unicode code points (e.g. mysql varchar(200) for a utf-8 database).

// ok if php.ini default_charset set to UTF-8 (= default value)
mb_strlen ( $value );
iconv_strlen ( $value );
// always ok
mb_strlen ( $value , «UTF-8» );
iconv_strlen ( $value , «UTF-8» );

// BAD, do not use:
strlen ( utf8_decode ( $value )); // breaks for some multi-byte characters
grapheme_strlen ( $value ); // counts graphemes, not code points
?>

2. The database column has a length defined in bytes (e.g. oracle’s VARCHAR2(200 BYTE))

// ok, but assumes mbstring.func_overload is 0 in php.ini (= default value)
strlen ( $value );
// ok, forces count in bytes
mb_strlen ( $value , «8bit» )
?>

3. The database column is in another character set (UTF-16, ISO-8859-1, etc. ) with a length defined in characters / code points.

Find the character set used, and pass it explicitly to the length function.

PHP’s strlen function behaves differently than the C strlen function in terms of its handling of null bytes (‘\0’).

In PHP, a null byte in a string does NOT count as the end of the string, and any null bytes are included in the length of the string.

In C, the same call would return 2.

Thus, PHP’s strlen function can be used to find the number of bytes in a binary string (for example, binary data returned by base64_decode).

We just ran into what we thought was a bug but turned out to be a documented difference in behavior between PHP 5.2 & 5.3. Take the following code example:

$attributes = array( ‘one’ , ‘two’ , ‘three’ );

if ( strlen ( $attributes ) == 0 && ! is_bool ( $attributes )) echo «We are in the ‘if’\n» ; // PHP 5.3
> else echo «We are in the ‘else’\n» ; // PHP 5.2
>

?>

This is because in 5.2 strlen will automatically cast anything passed to it as a string, and casting an array to a string yields the string «Array». In 5.3, this changed, as noted in the following point in the backward incompatible changes in 5.3 (http://www.php.net/manual/en/migration53.incompatible.php):

«The newer internal parameter parsing API has been applied across all the extensions bundled with PHP 5.3.x. This parameter parsing API causes functions to return NULL when passed incompatible parameters. There are some exceptions to this rule, such as the get_class() function, which will continue to return FALSE on error.»

So, in PHP 5.3, strlen($attributes) returns NULL, while in PHP 5.2, strlen($attributes) returns the integer 5. This likely affects other functions, so if you are getting different behaviors or new bugs suddenly, check if you have upgraded to 5.3 (which we did recently), and then check for some warnings in your logs like this:

strlen() expects parameter 1 to be string, array given in /var/www/sis/lib/functions/advanced_search_lib.php on line 1028

If so, then you are likely experiencing this changed behavior.

I would like to demonstrate that you need more than just this function in order to truly test for an empty string. The reason being that will return 0. So how do you know if the value was null, or truly an empty string?

$foo = null ;
$len = strlen ( null );
$bar = » ;

echo «Length: » . strlen ( $foo ) . «
» ;
echo «Length: $len
» ;
echo «Length: » . strlen ( null ) . «
» ;

if ( strlen ( $foo ) === 0 ) echo ‘Null length is Zero
‘ ;
if ( $len === 0 ) echo ‘Null length is still Zero
‘ ;

Null length is Zero
Null length is still Zero

!is_null(): $foo is probably null
isset(): $foo is probably null

!is_null(): $bar is truly an empty string
isset(): $bar is truly an empty string
// End Output

So it would seem you need either is_null() or isset() in addition to strlen() if you care whether or not the original value was null.

There’s a LOT of misinformation here, which I want to correct! Many people have warned against using strlen(), because it is «super slow». Well, that was probably true in old versions of PHP. But as of PHP7 that’s definitely no longer true. It’s now SUPER fast!

I created a 20,00,000 byte string (~20 megabytes), and iterated ONE HUNDRED MILLION TIMES in a loop. Every loop iteration did a new strlen() on that very, very long string.

The result: 100 million strlen() calls on a 20 megabyte string only took a total of 488 milliseconds. And the strlen() calls didn’t get slower/faster even if I made the string smaller or bigger. The strlen() was pretty much a constant-time, super-fast operation

So either PHP7 stores the length of every string as a field that it can simply always look up without having to count characters. Or it caches the result of strlen() until the string contents actually change. Either way, you should now never, EVER worry about strlen() performance again. As of PHP7, it is super fast!

Here is the complete benchmark code if you want to reproduce it on your machine:

$iterations = 100000000 ; // 100 million
$str = str_repeat ( ‘0’ , 20000000 );

// benchmark loop and variable assignment to calculate loop overhead
$start = microtime ( true );
for( $i = 0 ; $i < $iterations ; ++ $i ) $len = 0 ;
>
$end = microtime ( true );
$loop_elapsed = 1000 * ( $end — $start );

// benchmark strlen in a loop
$len = 0 ;
$start = microtime ( true );
for( $i = 0 ; $i < $iterations ; ++ $i ) $len = strlen ( $str );
>
$end = microtime ( true );
$strlen_elapsed = 1000 * ( $end — $start );

// subtract loop overhead from strlen() speed calculation
$strlen_elapsed -= $loop_elapsed ;

echo «\nstring length: < $len >\ntest took: < $strlen_elapsed >milliseconds\n» ;

Источник

PHP strlen() Function

The strlen() function returns the length of a string.

Syntax

Parameter Values

Technical Details

Return Value: Returns the length of a string (in bytes) on success, and 0 if the string is empty
PHP Version: 4+

More Examples

Example

Return the length of the string «Hello World»:

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

PHP Strings

A string is a sequence of characters, like «Hello world!».

PHP String Functions

In this chapter we will look at some commonly used functions to manipulate strings.

strlen() — Return the Length of a String

The PHP strlen() function returns the length of a string.

Example

Return the length of the string «Hello world!»:

str_word_count() — Count Words in a String

The PHP str_word_count() function counts the number of words in a string.

Example

Count the number of word in the string «Hello world!»:

strrev() — Reverse a String

The PHP strrev() function reverses a string.

Example

Reverse the string «Hello world!»:

strpos() — Search For a Text Within a String

The PHP strpos() function searches for a specific text within a string. If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE.

Example

Search for the text «world» in the string «Hello world!»:

Tip: The first character position in a string is 0 (not 1).

str_replace() — Replace Text Within a String

The PHP str_replace() function replaces some characters with some other characters in a string.

Example

Replace the text «world» with «Dolly»:

Complete PHP String Reference

For a complete reference of all string functions, go to our complete PHP String Reference.

The PHP string reference contains description and example of use, for each function!

PHP Exercises

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Читайте также:  Ajax $.Load Method Example of Loading Data from External File
Оцените статью