0

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?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Kns
  • 25
  • 5

1 Answers1

0

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

OMi Shah
  • 5,768
  • 3
  • 25
  • 34
  • 2
    Solution for PHP < 7 or since 4.0.6+ to latest. https://stackoverflow.com/a/14566547/128761 – vee Oct 16 '22 at 04:50