iconv_strlen

(PHP 5, PHP 7, PHP 8)

iconv_strlenRetorna a contagem de caracteres da string

Descrição

iconv_strlen(string $string, ?string $encoding = null): int|false

Ao contrário de strlen(), iconv_strlen() conta as ocorrências de caracteres na string de sequência de bytes fornecida com base no conjunto de caracteres especificado, cujo resultado não é necessariamente idêntico ao comprimento da string em bytes.

Parâmetros

string

A string.

encoding

Se o parâmetro encoding for omitido ou null, presume-se que a string esteja codificada em iconv.internal_encoding.

Valor Retornado

Retorna a contagem de caracteres da string, como um número inteiro, ou false se ocorrer um erro durante a codificação.

Registro de Alterações

Versão Descrição
8.0.0 encoding é anulável agora.

Veja Também

adicione uma nota

Notas Enviadas por Usuários (em inglês) 2 notes

up
13
hfuecks @ nospam org
19 years ago
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: 20strlen/utf8_decode: 20iconv_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);<?phpif (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-8Similiar applies to iconv_substr, iconv_strpos and iconv_strrpos
up
2
sheryl
5 years ago
Notice there is a disconnect:  >If charset`parameter is omitted, str is assumed to be encoded in iconv.internal_encoding.But clicking on the iconv.internal_encoding link (https://www.php.net/manual/en/iconv.configuration.php), the docs indicate that iconv.internal_encoding is deprecated since 5.6.
To Top