PHP
downloads | documentation | faq | getting help | mailing lists | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

iconv_strpos> <iconv_set_encoding
Last updated: Fri, 14 Nov 2008

view this page in

iconv_strlen

(PHP 5)

iconv_strlenRetourne le nombre de caractères d'une chaîne

Description

int iconv_strlen ( string $str [, string $charset ] )

À l'opposée de strlen(), la valeur de retour de iconv_strlen() est le nombre de caractères faisant partie de la séquence d'octets str , ce qui n'est pas toujours la même chose que la taille en octets de la chaîne de caractères.

Liste de paramètres

str

La chaîne de caractères.

charset

Si charset n'est pas passé en paramètre, str est supposée être encodée en iconv.internal_encoding.

Valeurs de retour

Retourne le nombre de caractères de la chaîne str , sous la forme d'un entier.

Voir aussi



add a note add a note User Contributed Notes
iconv_strlen
hfuecks @ nospam org
25-Feb-2006 01: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;

<?php
# UTF-8 string containing bad sequence: \xe9
$str = "Iñtërnâtiôn\xe9àlizætiøn";

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

Displays;

mb_strlen: 20
strlen/utf8_decode: 20
iconv_strlen:

(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);

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

A slower but stricter check (regex) can be found at: http://www.w3.org/International/questions/qa-forms-utf-8

Similiar applies to iconv_substr, iconv_strpos and iconv_strrpos

iconv_strpos> <iconv_set_encoding
Last updated: Fri, 14 Nov 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites