-1

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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Moe Joe
  • 75
  • 7

1 Answers1

2

Use mb_substr:

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

It will work properly with accenteded letters.

Output will be

Array (
    [0] => x
    [1] => bam
)
Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29