Php check string size

mb_strlen

The encoding parameter is the character encoding. If it is omitted or null , the internal character encoding value will be used.

Return Values

Returns the number of characters in string string having character encoding encoding . A multi-byte character is counted as 1.

Errors/Exceptions

If the encoding is unknown, an error of level E_WARNING is generated.

Changelog

See Also

  • mb_internal_encoding() — Set/Get internal character encoding
  • grapheme_strlen() — Get string length in grapheme units
  • iconv_strlen() — Returns the character count of string
  • strlen() — Get string length

User Contributed Notes 7 notes

If you are unsure about what $encoding can be set to, here’s a full list of all the encodings supported by this extension:

Speed of mb_strlen varies a lot according to specified character set.

If you need length of string in bytes (strlen cannot be trusted anymore because of mbstring.func_overload) you should use .
It’s the fastest way (still a way slower than strlen, though) to determine byte length of string. Other single byte character sets (ASCII, ISO-8859-1, . ) are several times slower than 8bit.

Just did a little benchmarking (1.000.000 times with lorem ipsum text) on the mbs functions

especially mb_strtolower and mb_strtoupper are really slow (up to 100 times slower compared to normal functions). Other functions are alike-ish, but sometimes up to 5 times slower.

Читайте также:  Html table to json convert to

just be cautious when using mb_ functions in high frequented scripts.

# test runs: 1000000
# benchmarking strlen vs. mb_strlen
# normal strlen: 3.6795361042023 ms, average: 3.6795361042023E-6 ms
# mb_strlen: 5.5934538841248 ms, average: 5.5934538841248E-6 ms
ok 1 — mb_strlen is slower than strlen
# mb_strlen is 1.52 slower than strlen
#
#
# benchmarking strpos vs. mb_strpos
# normal strpos: 5.5523281097412 ms, average: 5.5523281097412E-6 ms
# mb_strlen: 31.180974960327 ms, average: 3.1180974960327E-5 ms
ok 2 — mb_strlen is slower than strlen
# mb_strpos is 5.62 slower than strpos
#
#
# benchmarking substr vs. mb_substr
# normal substr: 3.4437320232391 ms, average: 3.4437320232391E-6 ms
# mb_strlen: 3.5374181270599 ms, average: 3.5374181270599E-6 ms
ok 3 — mb_strlen is slower than strlen
# mb_substr is 1.03 slower than substr
#
#
# benchmarking strtolower vs. mb_strtolower
# normal strtolower: 4.446839094162 ms, average: 4.446839094162E-6 ms
# mb_strlen: 193.44901108742 ms, average: 0.00019344901108742 ms
ok 4 — mb_strlen is slower than strlen
# mb_strtolower is 43.5 slower than strtolower
#
#
# benchmarking strtoupper vs. mb_strtoupper
# normal strtoupper: 3.0210740566254 ms, average: 3.0210740566254E-6 ms
# mb_strlen: 340.71775603294 ms, average: 0.00034071775603294 ms
ok 5 — mb_strlen is slower than strlen
# mb_strtoupper is 112.78 slower than strtoupper

It may not be clear whether PHP actually supports utf-8, which is the current de facto standard character encoding for Web documents, which supports most human languages. The good news is: it does.

I wrote a test program which successfully reads in a utf-8 file (without BOM) and manipulates the characters using mb_substr, mb_strlen, and mb_strpos (mb_substr should normally be avoided, as it must always start its search at character position 0).

The results with a variety of Unicode test characters in utf-8 encoding, up to four bytes in length, were mostly correct, except that accent marks were always mistakenly treated as separate characters instead of being combined with the previous character; this problem can be worked around by programming, when necessary.

If you find yourself without the mb string functions and can’t easily change it, a quick hack replacement for mb_strlen for utf8 characters is to use a a PCRE regex with utf8 turned on.

This is basically an ugly hack which counts all single character matches, and I’d expect it to be painfully slow on large strings.

Thank you Peter Albertsson for presenting that!

After spending more than eight hours tracking down two specific bugs in my mbstring-func_overloaded environment I have learned a very important lesson:

Many developers rely on strlen to give the amount of bytes in a string. While mb-overloading has very many advantages, the most hard-spotted pitfall must be this issue.

Two examples (from the two bugs found earlier):

1. Writing a string to a file:

$str = «string with utf-8 chars åèä — doo-bee doo-bee dooh» ;
$fp = fopen ( $this -> _file , «wb» );
if ( $fp ) $len = strlen ( $str );
fwrite ( $fp , $str , $len );
>
?>

PS This is found i the PEAR::Cache_Lite package (Lite.php) — Reported

2. Iterating through a string’s characters:

$str = «string with utf-8 chars åèö — doo-bee doo-bee dooh» ;
$newStr = «» ;
for ( $i = 0 ; $i < strlen ( $str ); $i ++) $newStr .= $str [ $i ];
>
?>

Both of these situations will fail to save / store the last characters in $str. This can be very hard to spot and can be especially fatal for say serialized strings, xml etc.

So, try to avoid these situations to support overloaded environments, and remeber Peter Albertssons remark if you find problems under such an environment.

I have been working with some funny html characters lately and due to the nightmare in manipulating them between mysql and php, I got the database column set to utf8, then store characters with html enity «ọ» as ọ in the database and set the encoding on php as «utf8».

This is where mb_strlen became more useful than strlen. While strlen(‘ọ’) gives result as 3, mb_strlen(‘ọ’,’UTF-8′) gives 1 as expected.

But left(column1,1) in mysql still gives wrong char for a multibyte string. In the example above, I had to do left(column1,3) to get the correct string from mysql. I am now about to investigate multibyte manipulation in mysql.

  • Multibyte String Functions
    • mb_​check_​encoding
    • mb_​chr
    • mb_​convert_​case
    • mb_​convert_​encoding
    • mb_​convert_​kana
    • mb_​convert_​variables
    • mb_​decode_​mimeheader
    • mb_​decode_​numericentity
    • mb_​detect_​encoding
    • mb_​detect_​order
    • mb_​encode_​mimeheader
    • mb_​encode_​numericentity
    • mb_​encoding_​aliases
    • mb_​ereg_​match
    • mb_​ereg_​replace_​callback
    • mb_​ereg_​replace
    • mb_​ereg_​search_​getpos
    • mb_​ereg_​search_​getregs
    • mb_​ereg_​search_​init
    • mb_​ereg_​search_​pos
    • mb_​ereg_​search_​regs
    • mb_​ereg_​search_​setpos
    • mb_​ereg_​search
    • mb_​ereg
    • mb_​eregi_​replace
    • mb_​eregi
    • mb_​get_​info
    • mb_​http_​input
    • mb_​http_​output
    • mb_​internal_​encoding
    • mb_​language
    • mb_​list_​encodings
    • mb_​ord
    • mb_​output_​handler
    • mb_​parse_​str
    • mb_​preferred_​mime_​name
    • mb_​regex_​encoding
    • mb_​regex_​set_​options
    • mb_​scrub
    • mb_​send_​mail
    • mb_​split
    • mb_​str_​split
    • mb_​strcut
    • mb_​strimwidth
    • mb_​stripos
    • mb_​stristr
    • mb_​strlen
    • mb_​strpos
    • mb_​strrchr
    • mb_​strrichr
    • mb_​strripos
    • mb_​strrpos
    • mb_​strstr
    • mb_​strtolower
    • mb_​strtoupper
    • mb_​strwidth
    • mb_​substitute_​character
    • mb_​substr_​count
    • mb_​substr

    Источник

    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.

    Источник

    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.

    Источник

Оцените статью