Add accented characters within an array

Asked

Viewed 122 times

-3

I have a variable that stores a name:

$nome = 'André Ramos';

I have a array that displays all 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 you enter the name in the variable $nome I need the array $letras check each character and display its value. You need to display the value of accentuated letters 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 someone help me ?

  • 1

    Start by studying the function mb_strlen

  • 1

    Hello, welcome to the OS, please take a [tour] through the platform to learn more how to get the best out of it and how to create questions that can result in help

2 answers

1


Your problem is basically walking through a string marked. The problem is that PHP, by default, will scroll through the characters at each byte, getting lost in multibyte characters, as in the case of accented letters. For example, strlen('andré') returns 6 instead of 5, because the letter é demand 2 bytes and the string integer has 6 bytes in total.

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,
) 

Documentation of functions for research:

  • thank you very much, that’s exactly what I was looking for. I’ll study more about!

-2

Follow 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);
 ?>
  • Could you explain your solution? Why use create_function, which is an obsolete function from PHP 7.2, to just call array_key_exists? It is not easier to call the function directly?

  • Yes, it is obsolete in version 7 and the manual indicates as an option the use of Anonymous Functions. $f_lambda = Function($a,$b) { Return array_key_exists($a,$b); };

  • And why do that? It’s no longer simple to do if (array_key_exists($a, $b))?

  • Thanks @Carlosmesquitaaguiar

  • Not at all @Js Dev!

  • @Carlosmesquitaaguiar, you can ask another question?

  • @Carlosmesquitaaguiar I edited the question, actually I need some other information, if I can take a look, thank you.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.