Moe Joe Moe Joe - 9 months ago 58
PHP Question

string mutation with accented letters in php code

I have a string replacement that includes accented letters. I also used already a normalizer so that I have the same encoding and I cannot remove the diacritics because I need them for my output. My Code:

$word = array("bā","ba");


$accented = array("ā","ē","ī","ō","ū");

$last = substr($word[$i],-1);

if ( in_array($last,$accented)) { // replacement of the array with the accented letters
$word[$i] = rtrim("x",$word[$i]);


How can I modify my code so that it works for accented letters?

Answer Source

Use mb_substr:

$last = mb_substr($word[$i],-1);

It will work properly with accenteded letters.

Output will be

Array (
    [0] => x
    [1] => bam