downloads | documentation | faq | getting help | mailing lists | reporting bugs | sites | links | my 
search for in the  
view the version of this page
Last updated: Sun, 02 May 2004


(PHP 3, PHP 4 , PHP 5)

strlen -- Get string length


int strlen ( string str)

Returns the length of string.

Esempio 1. A strlen() example

= 'abcdef';
strlen($str); // 6

$str = ' ab cd ';
strlen($str); // 7

See also count().

add a note add a note User Contributed Notes
dtorop932 at hotmail dot com
04-Dec-2003 06:03
A more succinct way to get strlen of UTF-8 string w/out the multi-byte string library:

$len = preg_match_all('/./u', $str, $dummy);

A side effect is the $dummy contains a UTF-8 character by character breakdown of the string...
dtorop932 at hotmail dot com
03-Dec-2003 08:25
To follow up on dr-strange's utf8_strlen(), here are two succinct alternate versions.  The first is slower for multibyte UTF-8, faster for single byte UTF-8.  The second should be much faster for all but very brief strings, and can easily live inline in code.  Neither validates the UTF-8.

Note that the right solution is to use mb_strlen() from the mbstring module, if one is lucky enough to have that compiled in...

// choice 1
function utf8_strlen($str) {
$count = 0;
  for (
$i = 0; $i < strlen($str); ++$i) {
   if ((
ord($str[$i]) & 0xC0) != 0x80) {

// choice 2
function utf8_strlen($str) {
preg_match_all('/[\x00-\x7F\xC0-\xFD]/', $str, $dummy);
26-Sep-2003 12:13
> a little modification of rasmus solution
> $tmp=0; $s="blah";
> while($c=$s[$tmp++]) { echo $c; }
> but what happens when the string contains zeros?
> $s="blah0blah";
> the script stops....

Then use this:
=0; $s="blah0blah";
ord($c = $s[$tmp++]) )
01-Aug-2003 08:36
Just a general pointer that I have hit upon after some struggle:

Most blobs can easily be treated as strings, so to retreat info on a blob or to manipulate it in any way, I recommend trying out string-related functions first. They've worked well for me.
suchy at ecl dot pl
13-May-2003 09:22
a little modification of rasmus solution

$tmp=0; $s="blah";
while($c=$s[$tmp++]) { echo $c; }

but what happens when the string contains zeros?
the script stops....

here is sample of code and the string is correcly parsed using strlen() even when containing zeros:

for (
alber at netdot dot ch
28-Jan-2003 11:26
If you are using php < 4.01 this function makes the same as
str_pad in php >=4.01

function str_pad($variable, $size)
   while (strlen($variable) < $size)
                     $variable = $variable ." "; //Add Space

     }//End While

  return $variable;

}//End Function

$NameVorname = "AlberMichael";
echo $pdf; //Returns AlberMichael
dr - strange at shaw dot ca
03-Oct-2002 01:08
It seems to me that all strings in PHP are ASCII, this is fine for some but for me I need more. I thought I would show off a small function that I made that will tell you the length of a UTF-8 string. This comes in handy if you want to restrict the size of user input to say 30 chars - but don't want to force ascii only input on your users.

function utf8_strlen($str)
   $count = 0;

   for($i = 0; $i < strlen($str); $i++)
       $value = ord($str[$i]);
       if($value > 127)
           if($value >= 192 && $value <= 223)
           elseif($value >= 224 && $value <= 239)
               $i = $i + 2;
           elseif($value >= 240 && $value <= 247)
               $i = $i + 3;
               die('Not a UTF-8 compatible string');
   return $count;
26-May-2001 06:11
Note that PHP does not need to traverse the string to know its length with strlen(). The length is an attribute of the array used to store the characters. Do not count on strings being terminated by a NULL character. This may not work with some character encodings, and PHP strings are binary-safe !
PHP4 included a NULL character after the last position in the string, however, this does not change the behavior of strlen or the binary safety: this NULL character is not stored.

However PHP4 allows now to reference the position after the end of the string for both read and write access:

* when reading at that position (e.g. $s[strlen($s)]), you get a warning with PHP3, and you'll get 0 with PHP4 not returning a warning.

* you can assign it directly in PHP4 with one character to increase the string length by one character:

$s[strlen($s)]=65; //append 'A'
$s[]=65; //append 'A'
$s[strlen($s)]=0; //append NUL (stored!)
$s[]=0; //append NUL (stored!)

Such code did not work in PHP3, where the only way to extend the string length was by using a concat operator.

However, reading or writing past the end of the string, using an array index superior to the current string length will still raise a warning in PHP4.
ben at _idataconnect dot com
01-Apr-2001 05:44
I think another thing that people aren't understanding about this is that PHP doesn't use pointers like C. The way you can treat string variables as arrays seems to be added for convenience. Like someone posted earlier, you shouldn't rely on the language to hit an error and end the loop. If you do things like that and someone changed the way PHP (or any language) worked with those kind of errors, your program just might not work anymore.
validus at c-gate dot net
15-Mar-2001 11:07
Several people here have commented on
the termination of strings in PHP with the NULL character.
My tests have shown that

PHP3 does NOT terminate strings
PHP4 DOES terminate with a NULL   

And one of the previous posters seemed to indicate that zero couldn't be a part of a string if it were NULL terminated.  It can.  A zero is ASCII 48
and a NULL character is ASCII 0.

rasmus at php dot net
03-Nov-2000 05:37
This bit of code works just fine in PHP:

$tmp=0; $s="blah";
while($c=$s[$tmp++]) { echo $c; }

Depending on your warning level you may get a warning when $tmp = 4 because you have gone beyond the end of the string.  If you don't care about this warning either change your warning level or simply swallow it using:

  $tmp=0; $s="blah";
  while(@$c=$s[$tmp++]) { echo $c; }

The @ in front of the assignment will swallow any warnings from that assignment.

 Last updated: Sun, 02 May 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This mirror generously provided by: Italia OnLine S.p.a.
Last updated: Fri May 21 04:11:23 2004 CEST