Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : String Functions : strlen

strlen

Get string length (PHP 4, PHP 5)
int strlen ( string string )

Example 2459. A strlen() example

<?php
$str
= 'abcdef';
echo
strlen($str); // 6

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

Related Examples ( Source code ) » strlen
















Code Examples / Notes » strlen

bradmwalker

want a predicate that tests a string for emptiness? use strlen instead of empty(). strlen only returns a false-equivalent value for ''.
example:
// takes string_array and returns an array without any values w/empty strings
function filter_empties ($string_array) {
   // note: the immensely retarded empty() function returns true on string '0'
   // use strlen as empty string predicate
   return count($string_array) ? array_filter ($string_array, 'strlen') : $string_array;
}


dtorop932

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...
<?php
// choice 1
function utf8_strlen($str) {
 $count = 0;
 for ($i = 0; $i < strlen($str); ++$i) {
   if ((ord($str[$i]) & 0xC0) != 0x80) {
     ++$count;
   }
 }
 return $count;
}
// choice 2
function utf8_strlen($str) {
 return preg_match_all('/[\x00-\x7F\xC0-\xFD]/', $str, $dummy);
}
?>


paolo dot mosna

Title: Strlen() ant bytes string lenght.
Just to remember that strlen() return the number of characters of a string. Often the strlen() function is used to compute the length in bytes of a string. This is correct until string is single byte encoded. If multi-byte char-set is used this constraint i no more verified. So when you require the number of bytes of a ASCII or UTF-8 encoded string, it is better to use following function:
   /**
    * Count the number of bytes of a given string.
    * Input string is expected to be ASCII or UTF-8 encoded.
    * Warning: the function doesn't return the number of chars
    * in the string, but the number of bytes.
    *
    * @param string $str The string to compute number of bytes
    *
    * @return The length in bytes of the given string.
    */
   function strBytes($str)
   {
     // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT
     
     // Number of characters in string
     $strlen_var = strlen($str);
 
     // string bytes counter
     $d = 0;
     
    /*
     * Iterate over every character in the string,
     * escaping with a slash or encoding to UTF-8 where necessary
     */
     for ($c = 0; $c < $strlen_var; ++$c) {
         
         $ord_var_c = ord($str{$d});
         
         switch (true) {
             case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
                 // characters U-00000000 - U-0000007F (same as ASCII)
                 $d++;
                 break;
             
             case (($ord_var_c & 0xE0) == 0xC0):
                 // characters U-00000080 - U-000007FF, mask 110XXXXX
                 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
                 $d+=2;
                 break;
 
             case (($ord_var_c & 0xF0) == 0xE0):
                 // characters U-00000800 - U-0000FFFF, mask 1110XXXX
                 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
                 $d+=3;
                 break;
 
             case (($ord_var_c & 0xF8) == 0xF0):
                 // characters U-00010000 - U-001FFFFF, mask 11110XXX
                 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
                 $d+=4;
                 break;
 
             case (($ord_var_c & 0xFC) == 0xF8):
                 // characters U-00200000 - U-03FFFFFF, mask 111110XX
                 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
                 $d+=5;
                 break;
 
             case (($ord_var_c & 0xFE) == 0xFC):
                 // characters U-04000000 - U-7FFFFFFF, mask 1111110X
                 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
                 $d+=6;
                 break;
             default:
               $d++;    
         }
     }
     
     return $d;
   }
This function has been adapted form the JSON function used to convert character in UTF-8 representation.
With this new function we solved problem in JSON and in PEAR/SOAP php libraries.


rasmus

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.


mike rosoft

The function "empty" tests whether a variable will evaluate as FALSE; the string '0' (as well as the number 0) does.
Another simple way of testing for an empty string is:
if($string=='') foo();
 else bar();
This will match an empty string and anything that converts to it (such as NULL/unset variable), but not the number zero as either a number or a string.


chernyshevsky

The easiest way to determine the character count of a UTF8 string is to pass the text through utf8_decode() first:
$length = strlen(utf8_decode($s));
utf8_decode() converts characters that are not in ISO-8859-1 to '?', which, for the purpose of counting, is quite alright.


hage yaapa

Sometimes you really wanna make sure no user edits the 'maxlength' attribute of the HTML page and POSTs a 5 Mb string to your script. Probably, advanced programmers already take precautions, this one is just a very simple tip for beginners on how to check the character lenth of the POST variables in an effective manner.
<?php
// ALWAYS clean the POST variables of any HTML tags first.
// And here we do it in one easy step.
$_POST = array_map('strip_tags', $_POST);

// These are the POST variables in the example
$alias = $_POST['alias'];
$name = $_POST['name'];
$status = $_POST['status'];
$year = $_POST['year'];

// We create an array that contains the expected character length
// for each POST variable
$exlen = array (
'alias'=>12,
'name'=>30,
'status'=>10,
'year'=>4
);

// Now check if any of them exceeds the expected length
foreach ($exlen as $key=>$val) {
  if (strlen($$key) > $val) {
    // The user has definitely edited the HTML! He has a lot of time, could be bad.
  // This section can edited according to your needs - very simple to complex.
  // Log the event or send an e-mail to the admin at the basic.
  // However, in this example we just print a warning.
  print 'WARNING: The FBI is looking for you, dude!';
  exit;
  // The best part is that the script won't look for any other
  // POST variables other than the ones which we are expecting already.
  }
}
?>
Similarly, with the use of Regular Expressions you could check the data type and string format too.


validus

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    
 character.  
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.
cheers
validus


26-may-2001 08: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.


mail4adry

my last post was a biiiiig mistake. This is the right way to display a few words of an html formatted text, which is based on the functions of the previous post, strllen_Html and strlen_noHtml, that returns the string length counting tag-characters and non-tag-characters.
This can be useful to put a preview of an article on the front page when the rest of the article is displayed on another page:
function getPreviewText($text) {
// Strip all tags
$desc = strip_tags(html_entity_decode($text), "<a><em>");
$charlen = 0; $crs = 0;
if(strlen_HTML($desc) == 0)
$preview = substr($desc, 0, 69);
else
{
$i = 0;
while($charlen < 80)
{
$crs = strpos($desc, " ", $crs)+1;
$lastopen = strrpos(substr($desc, 0, $crs), "<");
$lastclose = strrpos(substr($desc, 0, $crs), ">");
if($lastclose > $lastopen)
{
// we are not in a tag
$preview = substr($desc, 0, $crs);
$charlen = strlen_noHTML($preview);
}
$i++;
}
}
return $preview."&#8230;"
}
will display text cut as near as possible to character 80 respecting each <a> and <em> tags and ending with ...


packe100

Just a precisation, maybe obvious, about the strlen() behaviour: with binary strings (i.e. returned by the pack() finction) is made a byte count... so strlen returns the number of bytes contained in the binary string.

patrick

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.


dr - strange

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)
$i++;
elseif($value >= 224 && $value <= 239)
$i = $i + 2;
elseif($value >= 240 && $value <= 247)
$i = $i + 3;
else
die('Not a UTF-8 compatible string');
}

$count++;
}

return $count;
}


ben

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.

php

Hi,
if you want to trim a sentence to a certain number of
characters so that it is displayed nicely in a HTML page
(in a table for instance), then you actually want to count
the number of characters displayed rather than the
actual number of characters of the string.
For instance:
"L&agrave; bas" should really be 5 character long,
rather than 10.
Also you don't want to cut a special char in the middle.
For instance:
If 3 is the maximum number of characters,
"L&agrave; bas"  should be cut as "L&agrave; ..."
and not "L&a...";
So here is a simple method to dothat:
function nicetrim ($s) {
// limit the length of the given string to $MAX_LENGTH char
// If it is more, it keeps the first $MAX_LENGTH-3 characters
// and adds "..."
// It counts HTML char such as &aacute; as 1 char.
//
 $MAX_LENGTH = 22;
 $str_to_count = html_entity_decode($s);
 if (strlen($str_to_count) <= $MAX_LENGTH) {
   return $s;
 }
 $s2 = substr($str_to_count, 0, $MAX_LENGTH - 3);
 $s2 .= "...";
 return htmlentities($s2);
}


anpaza

Here's a better strlen() for UTF-8 strings that doesn't access the byte past end of the string (on which newer PHP barfs):
function strlen_utf8 ($str)
{
   $i = 0;
   $count = 0;
   $len = strlen ($str);
   while ($i < $len)
   {
$chr = ord ($str[$i]);
$count++;
$i++;
if ($i >= $len)
   break;
if ($chr & 0x80)
{
   $chr <<= 1;
   while ($chr & 0x80)
   {
$i++;
$chr <<= 1;
   }
}
   }
   return $count;
}


patrick a bierans de

function array_strlen(&$array,$fuse=200,$depth=0)
{
 // returns the strlen of all elements in a given array, an array inside
 // an array will add another 8 points
 // fuse: Recursion is limited to 200 calls, if you want unlimited calls
 //       use fuse=0 - warning: if an array element contains a reference
 //       to the array itself it will run endless if fuse set to 0 or below.
 $strlen=0;
 $fuse-=1;
 if ($fuse==0) return $strlen;
 if (is_array($array))
 {
   if ($depth>0) $strlen+=8;
   reset($array);
   $depth+=1;
   foreach ($array as $sub) $strlen+=array_strlen($sub,$fuse,$depth);
 }
 else
 {
   $strlen+=strlen($array);
 }
 return $strlen;
} // array_strlen()


http://nsk.wikinerds.org

Beware: strlen() counts new line characters at the end of a string, too!
 $a = "123\n";
 echo "

".strlen($a)."";
The above code will output 4.


zeldorblat

As an alternative to paolo dot mosna at gmail dot com's function, you can just use the built-in mb_strwidth() function:
http://www.php.net/mb_strwidth


liquix

An easy function to make sure the words in a sentence is not above the maximum lengt of characters. This is used to prevent that users posting for an example comments on your page and drag the page width out.
Returns true or false
<?php
function wordlength($txt, $limit)
{
  $words = explode(' ', $txt);
  foreach($words as $v)
  {
      if(strlen($v) > $limit)
      {
return false;
      }
  }
  return true;
}
?>
Uses like this:
<?php
$txt = "Onelongword and some small ones";
if(!wordlength($txt, 10))
{
die("One of the words where too long");
}
?>
That will return false since one of the words in $txt is too long. (Maximum set to 10)


triadsebas

A nice use of the strlen() function, the following function will check if one of the words in $input is longer than $maxlenght,  $maxlenght is standard 40.
<?php
function check_input($input, $maxlenght= 40)
$temp_array = explode(" ", $input);
foreach ($temp_array as $word) {
if (strlen($word) > $maxlenght) {
return false;
}
}
return true;
}
?>
Example:
<?php
if (!check_input($_POST['message'])) {
print 'One of your words in your message in longer than 40 chars. Please edit your message.';
}
?>


suchy

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....
here is sample of code and the string is correcly parsed using strlen() even when containing zeros:
<?
$s="blah0blah";
$si=0;
$s_len=strlen($s);
for ($si=0;$si<$s_len;$si++)
{
$c=$s[$si];
echo $c;
}
?>


topera

//------------------------------------------
// This function returns the necessary
// size to show some string in display
// For example:
// $a = strlen_layout("WWW"); // 49
// $a = strlen_layout("..."); // 16
// $a = strlen_layout("Hello World"); // 99
//------------------------------------------
function strlen_pixels($text) {
   /*
       Pixels utilized by each char (Verdana, 10px, non-bold)
       04: j
       05: I\il,-./:; <espace>
       06: J[]f()
       07: t
       08: _rz*
       09: ?csvxy
       10: Saeko0123456789$
       11: FKLPTXYZbdghnpqu
       12: AÇBCERV
       13: <=DGHNOQU^+
       14: w
       15: m
       16: @MW
   */
   // CREATING ARRAY $ps ('pixel size')
   // Note 1: each key of array $ps is the ascii code of the char.
   // Note 2: using $ps as GLOBAL can be a good idea, increase speed
   // keys:    ascii-code
   // values:  pixel size
   // $t: array of arrays, temporary
   $t[] = array_combine(array(106), array_fill(0, 1, 4));
   $t[] = array_combine(array(73,92,105,108,44), array_fill(0, 5, 5));
   $t[] = array_combine(array(45,46,47,58,59,32), array_fill(0, 6, 5));
   $t[] = array_combine(array(74,91,93,102,40,41), array_fill(0, 6, 6));
   $t[] = array_combine(array(116), array_fill(0, 1, 7));
   $t[] = array_combine(array(95,114,122,42), array_fill(0, 4, 8));
   $t[] = array_combine(array(63,99,115,118,120,121), array_fill(0, 6, 9));
   $t[] = array_combine(array(83,97,101,107), array_fill(0, 4, 10));
   $t[] = array_combine(array(111,48,49,50), array_fill(0, 4, 10));
   $t[] = array_combine(array(51,52,53,54,55,56,57,36), array_fill(0, 8, 10));
   $t[] = array_combine(array(70,75,76,80), array_fill(0, 4, 11));
   $t[] = array_combine(array(84,88,89,90,98), array_fill(0, 5, 11));
   $t[] = array_combine(array(100,103,104), array_fill(0, 3, 11));
   $t[] = array_combine(array(110,112,113,117), array_fill(0, 4, 11));
   $t[] = array_combine(array(65,195,135,66), array_fill(0, 4, 12));
   $t[] = array_combine(array(67,69,82,86), array_fill(0, 4, 12));
   $t[] = array_combine(array(78,79,81,85,94,43), array_fill(0, 6, 13));
   $t[] = array_combine(array(60,61,68,71,72), array_fill(0, 5, 13));
   $t[] = array_combine(array(119), array_fill(0, 1, 14));
   $t[] = array_combine(array(109), array_fill(0, 1, 15));
   $t[] = array_combine(array(64,77,87), array_fill(0, 3, 16));  
 
   // merge all temp arrays into $ps
   $ps = array();
   foreach($t as $sub) $ps = $ps + $sub;
 
   // USING ARRAY $ps
   $total = 1;
   for($i=0; $i<strlen($text); $i++) {
       $temp = $ps[ord($text[$i])];
       if (!$temp) $temp = 10.5; // default size for 10px
       $total += $temp;
   }
   return $total;
}
Rafael Pereira dos Santos


mail4adry

// Strip all tags except links
$desc = strip_tags(html_entity_decode($description), "<a>");
// Cut the line to the right length
if(strlen_nohtml($desc) > 72)
     $desc = substr($desc, 0 , 71+strlen_html($desc))."&#8230;";
output something like a text interrupted by ... at car #71 containing active links


bartek

> Just a precisation, maybe obvious, about the strlen() behaviour:
> with binary strings (i.e. returned by the pack() finction) is made
> a byte count... so strlen returns the number of bytes contained
> in the binary string.
This is not always true. strlen() might be shadowed by mb_strlen().
If that is the case it might treat binary data as unocode string and return wrong value (I just found it out after fighting with egroupware email attachment handling bug).
So, if your data is binary I would suggest using somthing like this (parts of the code from egroupware):
$has_mbstring = extension_loaded('mbstring') ||@dl(PHP_SHLIB_PREFIX.'mbstring.'.PHP_SHLIB_SUFFIX);
$has_mb_shadow = (int) ini_get('mbstring.func_overload');
if ($has_mbstring && ($has_mb_shadow & 2) ) {
  $size = mb_strlen($this->output_data,'latin1');
} else {
  $size = strlen($this->output_data);
}
--
Bartek


Change Language


Follow Navioo On Twitter
addcslashes
addslashes
bin2hex
chop
chr
chunk_split
convert_cyr_string
convert_uudecode
convert_uuencode
count_chars
crc32
crypt
echo
explode
fprintf
get_html_translation_table
hebrev
hebrevc
html_entity_decode
htmlentities
htmlspecialchars_decode
htmlspecialchars
implode
join
levenshtein
localeconv
ltrim
md5_file
md5
metaphone
money_format
nl_langinfo
nl2br
number_format
ord
parse_str
print
printf
quoted_printable_decode
quotemeta
rtrim
setlocale
sha1_file
sha1
similar_text
soundex
sprintf
sscanf
str_getcsv
str_ireplace
str_pad
str_repeat
str_replace
str_rot13
str_shuffle
str_split
str_word_count
strcasecmp
strchr
strcmp
strcoll
strcspn
strip_tags
stripcslashes
stripos
stripslashes
stristr
strlen
strnatcasecmp
strnatcmp
strncasecmp
strncmp
strpbrk
strpos
strrchr
strrev
strripos
strrpos
strspn
strstr
strtok
strtolower
strtoupper
strtr
substr_compare
substr_count
substr_replace
substr
trim
ucfirst
ucwords
vfprintf
vprintf
vsprintf
wordwrap
eXTReMe Tracker