php iconv translit for removing accents: not working as excepted?

后端 未结 7 1234
终归单人心
终归单人心 2020-12-10 06:09

consider this simple code:

echo iconv(\'UTF-8\', \'ASCII//TRANSLIT\', \'è\');

it prints

 `e

instead of ju

7条回答
  •  醉梦人生
    2020-12-10 06:54

    I have this standard function to return valid url strings without the invalid url characters. The magic seems to be in the line after the //remove unwanted characters comment.

    This is taken from the Symfony framework documentation: http://www.symfony-project.org/jobeet/1_4/Doctrine/en/08 which in turn is taken from http://php.vrana.cz/vytvoreni-pratelskeho-url.php but i don't speak Czech ;-)

    function slugify($text)
    {
      // replace non letter or digits by -
      $text = preg_replace('#[^\\pL\d]+#u', '-', $text);
    
      // trim
      $text = trim($text, '-');
    
      // transliterate
      if (function_exists('iconv'))
      {
        $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
      }
    
      // lowercase
      $text = strtolower($text);
    
      // remove unwanted characters
      $text = preg_replace('#[^-\w]+#', '', $text);
    
      if (empty($text))
      {
        return 'n-a';
      }
    
      return $text;
    }
    
    echo slugify('é'); // --> "e"
    

提交回复
热议问题