downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | sites | links | conferences | my

search for in the

iconv_strpos> <iconv_set_encoding
[edit] Last updated: Fri, 22 Jul 2011

view this page in


(PHP 5)

iconv_strlenReturns the character count of string


int iconv_strlen ( string $str [, string $charset = ini_get("iconv.internal_encoding") ] )

In contrast to strlen(), iconv_strlen() counts the occurrences of characters in the given byte sequence str on the basis of the specified character set, the result of which is not necessarily identical to the length of the string in byte.



The string.


If charset parameter is omitted, str is assumed to be encoded in iconv.internal_encoding.

Return Values

Returns the character count of str, as an integer.

See Also

add a note add a note User Contributed Notes iconv_strlen
hfuecks @ nospam org 25-Feb-2006 12:58
If iconv_strlen is passed a UTF-8 string containing badly formed sequences, it will return FALSE. This is in contrast to mb_strlen of the behaviour of utf8_decode, which strip out any bad sequences;

# UTF-8 string containing bad sequence: \xe9
$str = "Itrntin\xe9liztin";

"mb_strlen: ".mb_strlen($str,'UTF-8')."\n";
"strlen/utf8_decode: ".strlen(utf8_decode($str))."\n";
"iconv_strlen: ".iconv_strlen($str,'UTF-8')."\n";


mb_strlen: 20
strlen/utf8_decode: 20

(PHP 5.0.5)

As such it is being "stricter" than mb_strlen and it may mean you need to check for invalid sequences first. A quick way to check is to exploit the behaviour of the PCRE extension (see notes on pattern modifiers);

if (preg_match('/^.{1}/us',$str,$ar) != 1) {
"string contains invalid UTF-8");

A slower but stricter check (regex) can be found at:

Similiar applies to iconv_substr, iconv_strpos and iconv_strrpos

show source | credits | sitemap | contact | advertising | mirror sites