phpstringtext-extractionunicode-stringmultibyte

How can I print these PHP special characters?


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?


Solution

  • 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