• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • strlen()

    (PHP 4, PHP 5, PHP 7)

    获取字符串长度

    说明

    strlen (string $string ) : int

    返回给定的字符串 $string 的长度。

    参数

    $string

    需要计算长度的字符串。

    返回值

    成功则返回字符串 $string 的长度;如果 $string 为空,则返回 0。

    更新日志

    版本 说明
    5.3.0 Prior versions treated arrays as the string Array, thus returning a string length of 5 and emitting an E_NOTICE level error.

    范例

    strlen() 范例

    <?php
    $str = 'abcdef';
    echo strlen($str); // 6
    $str = ' ab cd ';
    echo strlen($str); // 7
    ?>

    注释

    Note:

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

    Note:

    strlen() returns NULL when executed on arrays, and an E_WARNING level error is emitted.

    参见

    • count()计算数组中的单元数目,或对象中的属性个数
    • mb_strlen()获取字符串的长度

    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().
    Example:
    <?php
    // the Arabic (Hello) string below is: 59 bytes and 32 characters
    $utf8 = "السلام علیکم ورحمة الله وبرکاته!";
    var_export( strlen($utf8) ); // 59
    echo "<br>";
    var_export( mb_strlen($utf8, 'utf8') ); // 32
    ?>
    The easiest way to determine the character count of a UTF8 string is to pass the text through utf8_decode() first:
    <?php
    $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.
    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:
    <?php
    $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.
    user johan at hultin dot se: if you consider the zeros to be padding, then there are 19 sets of three. thus, 19.
    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 <?php strlen(null); ?> will return 0. So how do you know if the value was null, or truly an empty string?
    <?php
    $foo = null;
    $len = strlen(null);
    $bar = '';
    echo "Length: " . strlen($foo) . "<br>";
    echo "Length: $len <br>";
    echo "Length: " . strlen(null) . "<br>";
    if (strlen($foo) === 0) echo 'Null length is Zero <br>';
    if ($len === 0) echo 'Null length is still Zero <br>';
    if (strlen($foo) == 0 && !is_null($foo)) echo '!is_null(): $foo is truly an empty string <br>';
    else echo '!is_null(): $foo is probably null <br>';
    if (strlen($foo) == 0 && isset($foo)) echo 'isset(): $foo is truly an empty string <br>';
    else echo 'isset(): $foo is probably null <br>';
    if (strlen($bar) == 0 && !is_null($bar)) echo '!is_null(): $bar is truly an empty string <br>';
    else echo '!is_null(): $foo is probably null <br>';
    if (strlen($bar) == 0 && isset($bar)) echo 'isset(): $bar is truly an empty string <br>';
    else echo 'isset(): $foo is probably null <br>';
    ?>
    // Begin Output:
    Length: 0
    Length: 0 
    Length: 0
    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:
    <?php
    $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";
    ?>
    if your text contain UTF8 character (arabic character for example), the mb_substr can use instead of strlen.
    but if the string contain UTF16 (wmoji, smily) then you can use this method to get correct length:
    <?php echo strlen(iconv('utf-8', 'utf-16le', 'ali'))/2;
    because some emoji (ex: ) is 2, 3 or 4 byte!
    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.
    For example, in PHP:
    strlen( "te\0st" ) = 5
    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).
    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).
    <?php
    // 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))
    <?php
    // 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
    // ok, supported charsets: http://php.net/manual/en/mbstring.supported-encodings.php
    mb_strlen($value, $charset);
    // ok, supported charsets: https://www.gnu.org/software/libiconv/
    iconv_strlen($value, $charset);
    ?>
    Attention with utf8:
    $foo = "bär";
    strlen($foo) will return 4 and not 3 as expected..
    Beware: strlen() counts new line characters at the end of a string, too!
    <?php
     $a = "123\n";
     echo "<p>".strlen($a)."</p>";
    ?>
    The above code will output 4.
    function limit_text( $text, $limit = 100000000000 ) {
        if ( strlen ( $text ) < $limit ) {
          return $text;
        }
        $split_words = explode(' ', $text );
        $out = null;
        foreach ( $split_words as $word ) {
          if ( ( strlen( $word ) > $limit ) && $out == null ) {
            return substr( $word, 0, $limit )."...";
          }
          
          if (( strlen( $out ) + strlen( $word ) ) > $limit) {
            return $out . "...";
          }      
          $out.=" " . $word;
        }
        return $out;
      }
    echo limit_text("hello world lorem ipsum",10);
    I use the string test for an inline error message. Hope others find it helpful.
      <span class="error" style="
       <?php if((isset($_POST['membershipID']) && strlen($_POST['membershipID']) < 5 ))
       {
        echo "display: inline-block;";
       } ?>">membership ID must be at least 5 digits</span>
    A strong advice: Never-Ever use "strlen" in intensive loop iterations (or similar), like this..
    for ($i = 0; $i < strlen($crc); $i++) .....
    It is notorious slow...
    This construct will use cpu each time to get the string length at each loop. Instead save the length in a variable and use that in the "for" loop. Note that You must not change the string length inside the loop then..).
    > 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):
    <?php
    $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
    When dealing with submitted forms that you've imposed a character limit on, you must remember that functions that count characters consider "\r\n" to be two characters.
    <?php
    //These will both output 2.
    echo strlen("\r\n");
    echo mb_strlen("\r\n");
    ?>
    If I had thought of this starting out, I would have saved myself several hours of trouble trying to get php to cut a message to the same length that my auxiliary javascript validation imposed on it.
    I've found this quite peculiar error (I think it might be truncating something, but not sure why and where that occurs)
    The string is the amount of unique shuffles of a deck of cards, and in it's raw form it's this 80658175170943878571660636856403766975289505440883277824000000000000
    if you however use strlen() to count this, it will return 19, which clearly the string is longer than
    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.

    上篇:stristr()

    下篇:strnatcasecmp()