If Encode (ISO-8859-1) is configured correctly the functions strlen()
and strrev()
work as expected, if using UTF-8 prefer the approach below.
stlen()
Does not count the number of characters but the byte, a practical example is input ação
returns 6(bytes) and not 4(characters), for multibite encoding characters use mb_strlen()
.
strrev()
suffers from the same problem, does not treat multibyte characters so does not do the inversion correctly, in which case use regex to solve the job.
Example with strlen()
& strrev()
$str = 'AÇÃO';
printf("%s - %s caracteres - invertido: %s", $str, strlen($str), strrev($str));
Satida:
AÇÃO - 6 caracteres - invertido: O�Ç�A
Example with regex & mb_strlen()
function mb_strrev($str){
preg_match_all('/./us', $str, $ar);
return implode('', array_reverse($ar[0]));
}
$str = 'AÇÃO';
printf("%s - %s caracteres - invertido: %s", $str, strlen($str), strrev($str));
Satida:
AÇÃO - 4 caracteres - invertido: OÃÇA
Julio, welcome to only. Your question was not within the standards of the site, so it was edited. If you do not agree, edit it, however, improving it. Visit How to create a good question to know how to ask a good question.
– user28595
Will not use any of these xD functions by amazing.
– rray