【问题标题】:Php Slugifiy function returned value is different in 2 serversPhp Slugfiy 函数返回值在 2 个服务器中不同
【发布时间】:2018-11-24 21:10:34
【问题描述】:

这是我的slugify 函数:

function slugify($text) {
    $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
    $text = trim($text, '-');
    $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
    $text = mb_strtolower($text, 'UTF-8');
    $text = preg_replace('~[^-\w]+~', '', $text);
    if(empty($text)) return 'n-a';
    return $text;
}

这是测试:

echo slugify("españa");

在我的开发服务器中,结果是:

  • 西班牙文

在我的生产服务器中,结果是:

  • espaa

我确定这与字符集编码有关,但两台服务器都将UTF-8 设为default_charset。我还能错过什么?有任何想法吗?

【问题讨论】:

    标签: php character-encoding preg-replace slugify


    【解决方案1】:

    问题来自iconv 函数。 在comments of the documentation,我们可以看到:

    请注意,当区域设置类别 LC_CTYPE 设置为 C 或 POSIX 时,iconv('UTF-8', 'ASCII//TRANSLIT', ...) 无法正常工作。您必须选择其他语言环境,否则所有非 ASCII 字符将被替换为问号。

    【讨论】:

      猜你喜欢
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      • 2015-11-22
      • 2014-10-07
      • 1970-01-01
      • 2013-08-05
      • 1970-01-01
      相关资源
      最近更新 更多