Replacing accented characters php

后端 未结 19 1371
鱼传尺愫
鱼传尺愫 2020-11-22 16:03

I am trying to replace accented characters with the normal replacements. Below is what I am currently doing.

    $string = \"Éric Cantona\";
    $strict = st         


        
19条回答
  •  温柔的废话
    2020-11-22 16:57

    To remove the diacritics, use iconv:

    $val = iconv('ISO-8859-1','ASCII//TRANSLIT',$val);
    

    or

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

    note that php has some weird bug in that it (sometimes?) needs to have a locale set to make these conversions work, using setlocale().

    edit tested, it gets all of your diacritics out of the box:

    $val = "á|â|à|å|ä ð|é|ê|è|ë í|î|ì|ï ó|ô|ò|ø|õ|ö ú|û|ù|ü æ ç ß abc ABC 123";
    echo iconv('UTF-8','ASCII//TRANSLIT',$val); 
    

    output (updated 2019-12-30)

    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
    

    Note that ð is correctly transliterated to d instead of o, as in the accepted answer.

提交回复
热议问题