【问题标题】:Remove all special chars, but not non-Latin characters删除所有特殊字符,但不删除非拉丁字符
【发布时间】:2018-04-16 15:49:44
【问题描述】:

我将这个 PHP 函数用于 SEO 网址。它适用于拉丁词,但我的网址是西里尔字母。此正则表达式 - /[^a-z0-9_\s-]/ 不适用于西里尔字符,请帮助我使其适用于非拉丁字符。

function seoUrl($string) {
    // Lower case everything
    $string = strtolower($string);
    // Make alphanumeric (removes all other characters)
    $string = preg_replace('/[^a-z0-9_\s-]/', '', $string);
    // Clean up multiple dashes or whitespaces
    $string = preg_replace('/[\s-]+/', ' ', $string);
    // Convert whitespaces and underscore to dash
    $string = preg_replace('/[\s_]/', '-', $string);
    return $string;
}

【问题讨论】:

  • 范围:U+0400–U+04FF
  • 谢谢,雷沃!这对我有用!
  • 请务必使用mb_strtolower 而不是strtolower

标签: php regex non-latin


【解决方案1】:

您需要为西里尔字母使用 Unicode 脚本,幸运的是 PHP PCRE 使用 \p{Cyrillic} 支持它。此外,您必须设置 u (unicode) 标志来预测引擎行为。您可能还需要i 标志来启用不区分大小写,例如A-Z:

~[^\p{Cyrillic}a-z0-9_\s-]~ui

你不需要双重转义\s。

PHP 代码:

preg_replace('~[^\p{Cyrillic}a-z0-9_\s-]+~ui', '', $string);

【讨论】:

    【解决方案2】:

    要了解有关 Unicode 正则表达式的更多信息,请参阅this article。

    \p{L} 或 \p{Letter} 匹配来自任何语言的任何类型的字母。

    要仅匹配西里尔字符,请使用\p{Cyrillic}

    由于西里尔字符不是标准的 ASCII 字符,您必须使用 u 标志/修饰符,因此正则表达式将根据需要识别 Unicode 字符。

    在使用 unicode 字符时,请务必使用 mb_strtolower 而不是 strtolower。

    因为您将所有字符都转换为小写,所以您不必使用i 正则表达式标志/修饰符。


    以下 PHP 代码应该适合您:

    function seoUrl($string) {
        // Lower case everything
        $string = mb_strtolower($string);
        // Make alphanumeric (removes all other characters)
        $string = preg_replace('/[^\p{Cyrillic}a-z0-9\s_-]+/u', '', $string);
        // Clean up multiple dashes or whitespaces
        $string = preg_replace('/[\s-]+/', ' ', $string);
        // Convert whitespaces and underscore to dash
        $string = preg_replace('/[\s_]/', '-', $string);
        return $string;
    }
    

    此外,请注意\p{InCyrillic_Supplementary} 匹配所有Cyrillic Supplementary characters,\p{InCyrillic} 匹配所有non-Supplementary Cyrillic characters。

    【讨论】:

      猜你喜欢
      • 2014-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多