【问题标题】:Replacing special characters from URL with iconv doesn't work用 iconv 替换 URL 中的特殊字符不起作用
【发布时间】:2015-04-12 00:56:29
【问题描述】:

我的一些网址有一个小问题。假设that $result['title'] = Citroën

在我的网址中,我希望这个词变成“雪铁龙”。下面的函数做的一切都是正确的,除了它删除了“ë”,所以我的网址变成了“citron”。

<?php echo strtolower(preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $result['title'])));?>

我想我可以通过使用iconv...来解决这个问题,但它不起作用。 “Citroën”仍被“citron”取代。

<?php echo strtolower(preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', iconv('UTF-8', 'ASCII//TRANSLIT', $result['title']))));?>

那么,我在这里错过了什么?

【问题讨论】:

  • 您是否考虑使用 urlencode() 代替。 echo urlencode('Citroën'); //Citro%C3%ABn
  • 使用 urlencode 给我“citroc3ab”。
  • 我找到了答案。我需要设置一个目标语言环境。 php setlocale(LC_ALL, 'en_GB');
  • ë 更改为 e 就像将 h 更改为 f 那么为什么不正确为此目的进行编码,对于网址,其urlencode()

标签: php string url url-rewriting iconv


【解决方案1】:

好的,我想通了。我需要设置一个目标语言环境。以下代码有效(因此“Citroën”变为“citroen”):

<?php setlocale(LC_ALL, 'en_GB.utf8'); echo strtolower(preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', iconv('UTF-8', 'ASCII//TRANSLIT', $result['titel']))));?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 2014-11-03
    • 2011-06-15
    • 2019-09-23
    • 2010-10-06
    • 1970-01-01
    相关资源
    最近更新 更多