Add characters with accents inside an array [closed]

-3

I have a variable that stores a name:

$nome = 'André Ramos';

I have an array that displays all the letters:

$letras = array (

            'A' => 1,
            'B' => 2,
            'C' => 3,
            'D' => 4,
            'F' => 8,
            'G' => 3,
            'H' => 5,
            'I' => 1,
            'J' => 1,
            'K' => 2,
            'L' => 3,
            'M' => 4,
            'N' => 5,
            'O' => 7,
            'P' => 8,
            'Q' => 1,
            'R' => 2,
            'S' => 3,
            'T' => 4,
            'U' => 6,
            'V' => 6,
            'X' => 6,
            'W' => 6,
            'Y' => 1,
            'Z' => 7,
            'Á' => 5,
            'É' => 4,
            'Í' => 8,
            'Ó' => 6,
            'Ú' => 3,
            'Ã' => 7,
            'Ñ' => 2

);

When typing the name in the variable $nome , I need the array $letras to check each character and display its value. You need to display the accented letter value as well.

For example:

$nome: 'André';

array (size=5)
  0 => int 1
  1 => int 5
  2 => int 4
  3 => int 2
  4 => int 4

Can anyone help me?

    
asked by anonymous 19.12.2018 / 12:54

2 answers

1

Your problem basically consists of traversing an accented string . The problem is that PHP, by default, will traverse the characters to each byte, getting lost in multibyte characters, as in the case of accented letters. For example, strlen('andré') returns 6 instead of 5, since the letter é requires 2 bytes and the integer string has 6 bytes in all.

As discussed in:

You can do:

$nome = 'andré';

if (preg_match_all('/./u', $nome, $caracteres) !== false) {
    $numeros = array_map(function ($letra) use ($letras) {
        return $letras[mb_strtoupper($letra)];
    }, $caracteres[0]);

    var_export($numeros);
}

The result will be:

array (
  0 => 1,
  1 => 5,
  2 => 4,
  3 => 2,
  4 => 4,
) 

Search function documentation:

20.12.2018 / 18:21
-1

Here's the answer man ...

<?php
    header('Content-Type: text/html; charset=utf-8');

    $encoding = mb_internal_encoding();
    $f_lambda = create_function('$a,$b', 'return array_key_exists($a,$b);');
    $arr_str_acentuados = array('Á' => 0,'É' => 0, 'Í' => 0, 'Ó' => 0, 'Ú' => 0);
    $frase = "É uma fraude! Como é fácil identificar essa coisas! Ótima solução!";
    $str_arr = str_split(mb_strtoupper(utf8_decode($frase),$encoding));

    foreach ($str_arr as $v)
       if($f_lambda(utf8_encode($v),$arr_str_acentuados))
          $arr_str_acentuados[utf8_encode($v)]++;


    print_r($arr_str_acentuados);
 ?>
    
19.12.2018 / 17:07