Moe Joe Moe Joe - 12 days ago 5
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");

for($i=0;$i<count($word);$i++)
{

$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

Use mb_substr:

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

It will work properly with accenteded letters.

Output will be

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