You can also use mb_substr
, or even regex.
$frase = 'nós';
$tamanho = mb_strlen($frase);
for($posicao = 0; $posicao < $tamanho; $posicao++){
echo mb_substr($frase, $posicao, 1, "UTF-8") . '-';
}
Try this here
In this way the mb_substr
will get the letter of the $posicao
, as it is set to UTF-8 there is no problem with multibytes characters.
This is also compatible with phrases like 您是否使用了翻译
, it will return:
您-是-否-使-用-了-翻-译-
The other responses, using utf8_decode, would return ?-?-?-?-?-?-?-?-
.
Another case would be to create something like str_split
, but compatible with characters with more than one byte, does not exist mb_str_split .
You could use preg_split
.
$frase = 'nós';
$tamanho = mb_strlen($frase);
$letras = array_filter(preg_split('//u', $frase));
foreach($letras as $letra){
echo $letra . '-';
}
Try this here.
In this case, preg_split
is responsible for creating an array, for each Unicode, so it will create one:
array(5) {
[0]=>
string(0) ""
[1]=>
string(1) "n"
[2]=>
string(2) "ó"
[3]=>
string(1) "s"
[4]=>
string(0) ""
}
Since we do not want empty spaces, we remove them with array_filter
.