4
I’m doing some tests and I realized that when the string has special characters it is counted more than one in substr
.
Example:
$string = "PAÇOCA";
echo strlen($string);
echo substr($string, 0, 3);
Should print: PAÇ
but only prints PA
, now if I increase a size from 3 to 4 prints, and if I take the Ç
and put a C
, he counts correctly, so from what I understand he’s considering the Ç
as if it were two characters, as I can count them correctly?
already tried using mb_string as well. and header with UTF8.
It worked for me: http://ideone.com/PRGNGR
– Maniero
account the same thing. convert so $string = "PAÇOCA"; $a = utf8_decode($string ) echo utf8_encode(substr($a , 0, 3));
– denis
mb_string functions are sufficient, as long as they are set to UTF-8
– Bacco
You look like a duplicate
– Wallace Maxters
strlen()
returns the number of bytes of the string (if the number of characters is lucky),mb_strlen()
returns the number of characters in the string.– rray
Related or duplicated : http://answall.com/questions/78308/por-que-deveria-utilizar-fun%C3%A7%C3%B5es-que-come%C3%A7am-by-Mb
– Wallace Maxters
@Wallacemaxters I think is more related, in case he tried to use mb_ but it did not help, and that does not focus on configuration. But it’s a good supplementary indication
– Bacco
@Bacco is true. But maybe because he had to pass the third 4 parameter, right, rsrsrsrsrsrsrsrsrsrs.
– Wallace Maxters
Now I’m thinking, using replace in 80% of my project, I’ll have to modify everything.O
– Gabriel Rodrigues
@Gabrielrodrigues if your encoding is UTF, yes. If it’s ISO, you can let the substr. And in situations that are byte operation, always overwrite without mb_ (for example, extract encoded things, or binary) - And look at the link I put to ini settings, it’s better than changing internal_encoding in Runtime.
– Bacco