Replace diacritic characters with “equivalent” ASCII in PHP?

前端 未结 4 1115
情深已故
情深已故 2021-01-31 20:42

Related questions:

  1. How to replace characters in a java String?
  2. How to replace special characters with their equivalent (such as " á " for &
4条回答
  •  忘了有多久
    2021-01-31 21:11

    The iconv module can do this, more specifically, the iconv() function:

    $str = iconv('Windows-1252', 'ASCII//TRANSLIT//IGNORE', "Gracišce");
    echo $str;
    //outputs "Gracisce"
    

    The main hassle with iconv is that you just have to watch your encodings, but it's definitely the right tool for the job (I used 'Windows-1252' for the example due to limitations of the text editor I was working with ;) The feature of iconv that you definitely want to use is the //TRANSLIT flag, which tells iconv to transliterate any characters that don't have an ASCII match into the closest approximation.

提交回复
热议问题