-1

I have a problem when I try to replace special chars like "Ñ" by "N" or "Ç" by "C" or "Ü" by "U" for example in PHP from strings latin1 codified.

I retrieve from mySQL which is UTF-8 codified this string "IBAÑEZ RIBERA, ALBA" and I would like to replace "Ñ" by "N" and get "IBANEZ RIBERA, ALBA", but I have several problems.

If I use str_replace it doesn't work and If I use iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', "IBAÑEZ RIBERA, ALBA") what I get is "IBAEZ RIBERA, ALBA", where this char "Ñ" is removed and not replaced by "N" how I like it.

So, how can I do this replacement?

Edit I:

I have observed that if I replace chars with mySQL functions in mySQL Workbench whit this query:

select id, id_player, name, replace(replace(replace(replace(name, " ", "-"), ",", " "), " ", ""), 'Ñ', 'N') as urlName 
from tbl003_player p, (select id_player from tbl006_player_club where id = 188) pc where 
p.id = pc.id_player;

I get the result what I want in the field "urlName"!!!

enter image description here

But, when I try to retrieve this data in PHP from the query I've got:

"IBAÑEZ-RIBERA-ALBA"

What happened?

halfer
  • 19,824
  • 17
  • 99
  • 186
José Carlos
  • 2,850
  • 12
  • 59
  • 95
  • This should cover everything lol https://stackoverflow.com/a/10790734/4802649 – Phiter Jan 05 '18 at 17:40
  • https://stackoverflow.com/q/25465114/1531971 –  Jan 05 '18 at 17:44
  • Good luck with that. There's a billion plus one variants of the letter N alone: Ń, Ň, , , , , ,⒩ ǹ, Ṅ, ᴺ, Ņ, ņ, ṇ, , ... – tadman Jan 05 '18 at 17:56
  • Thank you @Phiter, but it doesn't works :( If I use remove_accents function I get this string "IBAÑEZ RIBERA, ALBA" – José Carlos Jan 05 '18 at 18:12
  • Thank you @jdvr, but it doesn't works :( If I decode first and apply remove_accents function I get "IBA?EZ RIBERA, ALBA" and If I use str_replace(utf8_decode("IBAÑEZ RIBERA, ALBA")) I get an empty string :( – José Carlos Jan 05 '18 at 18:12
  • Bottom line: Don't try to use MySQL for such a translation. – Rick James Jan 07 '18 at 21:31

1 Answers1

1

Try this:

<?php

$string = 'IBAÑEZ RIBERA, ALBA, á â à å ä ð é ê è ë í î ì ï ó ô ò ø õ ö ú û ù ü æ ç ß abc ABC 123';

$value = iconv('UTF-8','ASCII//TRANSLIT',$string);

echo $value;
?>

Will produces the output:

IBANEZ RIBERA, ALBA, a a a a a d e e e e i i i i o o o o o o u u u u ae c ss abc ABC 123

UPDATE:

Since none of my sugests in this answer or in comments are working, use this:

<?php

$string = 'IBAÑEZ RIBERA, ALBA'; 

function changeChar($string){

$chars = array(
    'À'=>'A','Á'=>'A','Â'=>'A','Ã'=>'A','Ä'=>'A','Å'=>'A',
    'Æ'=>'A','Ç'=>'C','È'=>'E','É'=>'E','Ê'=>'E','Ë'=>'E',
    'Ì'=>'I','Í'=>'I','Î'=>'I','Ï'=>'I','Ð'=>'Dj','Ñ'=>'N',
    'Ò'=>'O','Ó'=>'O','Ô'=>'O','Õ'=>'O','Ö'=>'O','Ø'=>'O',
    'Ù'=>'U','Ú'=>'U','Û'=>'U','Ü'=>'U','Ý'=>'Y','Þ'=>'B',
    'ß'=>'Ss','à'=>'a','á'=>'a','â'=>'a','ã'=>'a','ä'=>'a',
    'å'=>'a','æ'=>'a','ç'=>'c','è'=>'e','é'=>'e','ê'=>'e',
    'ë'=>'e','ì'=>'i','í'=>'i','î'=>'i','ï'=>'i','ð'=>'o',
    'ñ'=>'n','ò'=>'o','ó'=>'o','ô'=>'o','õ'=>'o','ö'=>'o',
    'ø'=>'o','ù'=>'u','ú'=>'u','û'=>'u','ü'=>'u','ý'=>'y',
    'þ'=>'b','ÿ'=>'y','Ă'=>'A','ă'=>'a','Ń'=>'N','ń'=>'n',
    'Š'=>'S','š'=>'s','Ž'=>'Z','ž'=>'z','ƒ'=>'f','Ș'=>'S',
    'ș'=>'s','Ț'=>'T','ț'=>'t',
);
    return strtr($string, $chars);
    //or return str_replace(array_keys($chars), array_values($chars), $string);
}

echo changeChar($string);

It's not beautiful, but will work in most of cases.

Leonardo
  • 791
  • 1
  • 5
  • 21
  • This naïve approach only scratches the surface. Where's ø? ß? Ł? Code like this gives false confidence. – tadman Jan 05 '18 at 17:57
  • There might be limitations to this approach, but using `ASCII//TRANSLIT` seems like a much better plan. – tadman Jan 05 '18 at 18:48
  • Than you @Leonardo but it doesn't work. If I use iconv("UTF-8", "ASCII//TRANSLIT", "IBAÑEZ RIBERA, ALBA") I get an empty string. I don't undestand anything :( – José Carlos Jan 05 '18 at 18:55
  • Try iconv('UTF-8','ASCII//TRANSLIT//IGNORE',$string); – Leonardo Jan 05 '18 at 19:34
  • If it still not works, try to set your locale before iconv, like setlocale(LC_ALL, "en_US.utf8"), if you are using en_US. – Leonardo Jan 05 '18 at 20:12
  • Thank you @Leonardo but setting setlocal doesn't work to me :( If I do set_locale("LC_ALL", "es_ES.UTF8") and then iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", $n) returns me "IBAEZ RIBERA, ALBA" where the char "Ñ" is removed and not replaced by "N". I remember that my mySQL database is "latin1" codified. – José Carlos Jan 05 '18 at 21:07
  • Please, tell me if this function works in your environment. – Leonardo Jan 09 '18 at 15:25