Replace foreign characters

后端 未结 4 1115
时光说笑
时光说笑 2021-01-04 23:59

I need to be able to replace some common foreign characters with English equivalents before I store values into my db.

For example: æ replace with <

相关标签:
4条回答
  • 2021-01-05 00:41

    Excuse me second-guessing why you're doing this, but..

    If this is for search matching: The point of character set collation in a MySQL (for example), is that you can search for "n" and still match "ñ"

    IF this is for display purposes: I'd recommend if you have to do this, you do it when you display the text to a user. You can never get your original data back otherwise.

    0 讨论(0)
  • 2021-01-05 00:42

    For single character of accents

    $str = strtr($str, 
      "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÝßàáâãäåçèéêëìíîïñòóôõöøùúûüýÿ",
      "AAAAAACEEEEIIIINOOOOOOYSaaaaaaceeeeiiiinoooooouuuuyy"); 
    

    For double character of accents (such as Æ, æ)

    $match   = array('æ', 'Æ');
    $replace = array('ae', 'AE');
    $str = str_replace($replace, $replace, $str);
    
    0 讨论(0)
  • 2021-01-05 00:43

    You can define your convertable characters in an array, and use str_replace():

    $conversions = array(
        "æ" => "ae",
        "ñ" => "n",
    );
    
    $text = str_replace(array_keys($conversions), $conversions, $text);
    
    0 讨论(0)
  • 2021-01-05 00:43

    You can try iconv() with ASCII//TRANSLIT:

    $text = iconv("UTF-8", "ASCII//TRANSLIT", $text);
    
    0 讨论(0)
提交回复
热议问题