Fylax
Fylax

Reputation: 1128

PHP Replace Accents

I have this code:

$accents = ["/[Àà]/", "/[ÈÉèé]/", "/[Ìì]/", "/[Òò]/", "/[Ùù]/"];
$replacement = ["A", "E", "I", "O", "U"];
$to_be_replaced = preg_replace($accents, $replacement, $to_be_replaced);

It is intended to replace all accents (only the ones used in italian) with unaccented letters.

I tried with this:

$to_be_replaced = 'ò'; #first try
$to_be_replaced = 'èàò'; #second try

But I get this output:

1: AO

2: AEAAAO

So it seems to be adding an 'A' everytime before right replace, but I can't really figure out why.

Any suggestion?

Upvotes: 1

Views: 686

Answers (2)

Khawer Zeshan
Khawer Zeshan

Reputation: 9646

You can also use str_replace

<?php
    $string="Àkkk";
    $from = explode (',', "À,È,É,Ì,Ò,Ù,ç,æ,œ,á,é,í,ó,ú,à,è,ì,ò,ù,ä,ë,ï,ö,ü,ÿ,â,ê,î,ô,û,å,e,i,ø,u");
    $to = explode (',',"A,E,E,I,O,U,c,ae,oe,a,e,i,o,u,a,e,i,o,u,a,e,i,o,u,y,a,e,i,o,u,a,e,i,o,u");
    echo str_replace ($from, $to, $string);
?>

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

Encoding.

Try adding the u modifier to your regexes, ie "/[Àà]/u"

Upvotes: 3

Related Questions