Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the PHP functions iconv_strlen, mb_strlen and strlen?

Tags:

function

php

What is the difference between the iconv_strlen, mb_strlen and strlen functions?

And what is each used for in the real world?

like image 959
HELP Avatar asked May 17 '11 08:05

HELP


People also ask

What is the difference between mb_strlen and strlen in PHP?

Function strlen() is used when we require the string length showing a number of bytes whereas function mb_strlen() is used when we require the string length showing number of characters. The mb in the mb_strlen() function stands for multibyte.

What is mb_ strlen in PHP?

In PHP, multibyte string length (mb_strlen) function is used to get the total string length of a specified string. This function is supported in PHP 4.6. 0 or higher versions.

Which function in PHP is used to get the length of string variable?

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

How do I count letters in PHP?

The strlen() is a built-in function in PHP which returns the length of a given string. It takes a string as a parameter and returns its length. It calculates the length of the string including all the whitespaces and special characters.


1 Answers

The difference of strlen() to mb_strlen() is, that the second respects multibyte characters. This means, that this is the real character count. The first one assumes, that the string is always in ascii, what also means, that it always returns the size in bytes (very useful when handling binary "strings").

As far as I can see iconv_strlen() is quite similar to mb_strlen(), but fails on bad character sequences, in contrast to mb_strlen(), that just ignores them. And it uses the iconv-library (obviously ;)).

like image 156
KingCrunch Avatar answered Sep 28 '22 06:09

KingCrunch