1

I have problems with strings similar to these that have accents In the Initial Word.

for example : (nesecito grabs the first letter of each word of that name.)

$string =  'Álvaro Álvarez';
$parts = explode(" ", $string);
foreach($parts as $part){
    echo $part[0].'<br>';
}

I have this result for each letter: � �

if i use utf8_encode i have this as a result for each letter : Ã Ã but those are not the letters, the letters are Á Á

Here is an example of what I did.

example online

The result I'm looking for is Á Á If someone can help me solve this dilemma, I will be grateful.

Jean
  • 13
  • 5
  • In your example, You `echo` the `$part[0]` two times. So it gives this kind of results. Just remove the first echo. – Yash Parekh Jul 27 '18 at 05:10
  • So the issue with characters like `Á` is that they are multi-byte. Accessing PHP string characters via index assumes single-byte characters so you're only getting half of each character. As a test, try `echo substr($part, 0, 2)` instead :) – Phil Jul 27 '18 at 05:24
  • ... but please don't do that in production. Use `mb_substr()` as mentioned in the linked duplicate – Phil Jul 27 '18 at 05:29
  • thanks Phil!! ,Thanks for taking the time and for the help. – Jean Jul 27 '18 at 20:01

1 Answers1

0

Put this in your HTML header

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

If it didn't solve your problem try adding this at the beginning of your php file

header('Content-Type: text/html; charset=UTF-8');
Ace
  • 1
  • 2