简体   繁体   English

PHP替换口音

[英]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 1:AO

2: AEAAAO 2:AEAAAO

So it seems to be adding an 'A' everytime before right replace, but I can't really figure out why. 因此,似乎在每次正确替换之前每次都添加一个“ A”,但我真的不知道为什么。

Any suggestion? 有什么建议吗?

Encoding. 编码。

Try adding the u modifier to your regexes, ie "/[Àà]/u" 尝试在您的正则表达式中添加u修饰符,即"/[Àà]/u"

You can also use str_replace 您也可以使用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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM