php iconv转换为删除重音符号:不能按例外方式工作? [英] php iconv translit for removing accents: not working as excepted?

查看:82
本文介绍了php iconv转换为删除重音符号:不能按例外方式工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下简单代码:

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

它打印

 `e

不只是

 e

你知道我在做什么错吗?

do you know what I am doing wrong?

添加setlocale后没有任何改变

nothing changed after adding setlocale

setlocale(LC_COLLATE, 'en_US.utf8');
echo iconv('UTF-8', 'ASCII//TRANSLIT', 'è');

推荐答案

我具有此标准功能,可以返回有效的URL字符串,而不包含无效的url字符.魔术似乎在//删除不需要的字符注释之后.

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.

这取自Symfony框架文档: http://www .symfony-project.org/jobeet/1_4/Doctrine/zh/08 反过来取自 http://php.vrana.cz/vytvoreni-pratelskeho-url.php 但我不会说捷克语;-)

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"

这篇关于php iconv转换为删除重音符号:不能按例外方式工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆