How can I print these PHP special characters?

146 Views Asked by At

I have this piece of code:

<?php
$letters='ñéáóúí';
$letters=$letters[1];
echo $letters;
?>

I want to print "ñ" or any other letter but I only print � instead, any tips?

1

There are 1 best solutions below

1
OMi Shah On BEST ANSWER

You can use the mb_str_split() function provided by the PHP language to split multibyte characters string into individual characters.

Requires PHP 7 >= 7.4.0, PHP 8

<?php

$letters = 'ñéáóúí';

$letters = mb_str_split($letters)[1];

echo $letters; // ñ

?>

Live Demo: https://3v4l.org/sG8nQ