PHP - Problem with string accentuation in a string

2

I'm having problem with looping in strings with special characters, could anyone help me?

Follow the example:

$letras = 'nós';
$numero = strlen($letras);
for($j = 0; $j < $numero; $j++){
    echo $letras[$j]."-";
}

The result of the above code follows: link

    
asked by anonymous 02.06.2017 / 16:46

3 answers

2

Try to do this:

$letras = utf8_decode('nós');
$numero = strlen($letras);
for($j = 0; $j < $numero; $j++){
   echo utf8_encode($letras[$j])."-";
}
    
02.06.2017 / 16:51
2

Use utf8_decode to convert the string and then utf8_encode to make the correct impression.

 <?php
    $letras = utf8_decode('nós');
    $numero = strlen($letras);
    for($j = 0; $j < $numero; $j++){
        echo utf8_encode($letras[$j]."-");
    }
    
02.06.2017 / 16:51
2

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 .

    
02.06.2017 / 19:07