改进将字符串转换为可读的URL

时间:2012-06-12 13:03:30

标签: php regex url url-rewriting slug

以下函数会重写包含各种字符的新闻和产品标题中的网址。我想创建的字符串只包含字母数字值和“ - ”,但没有结尾“ - ”或空格,没有重复的“ - ”。下面的函数工作正常,但我想知道是否有任何方法可以更简单或更有效地编写它?

function urlName($string) {
    $string = trim($string);                          // no open ends
    $string = strtolower($string);                    // all lowercase
    $string = strtr($string, 'äöåÄÖÅ', 'aoaaoa');     // substitute umlauts
    $string = preg_replace('/[\W]+/', '-', $string);  // substitute non-word characters with -
    $string = preg_replace('/^-*|-*$/', '', $string); // no beinging or ending -
    return $string;
}

2 个答案:

答案 0 :(得分:1)

我经常使用这个:

function simpleText($s) {
    $rpl = array(
        "À" => 'A', "Á" => 'A', "Â" => 'A', "Ã" => 'A', "Ä" => 'A', "Å" => 'A',
        "à" => 'a', "á" => 'a', "â" => 'a', "ã" => 'a', "ä" => 'a', "å" => 'a',
        "Ò" => 'O', "Ó" => 'O', "Ô" => 'O', "Õ" => 'O', "Ö" => 'O', "Ø" => 'O',
        "ò" => 'o', "ó" => 'o', "ô" => 'o', "õ" => 'o', "ö" => 'o', "ø" => 'o',
        "È" => 'E', "É" => 'E', "Ê" => 'E', "Ë" => 'E',
        "è" => 'e', "é" => 'e', "ê" => 'e', "ë" => 'e',
        "Ç" => 'C',
        "ç" => 'c',
        "Ì" => 'I', "Í" => 'I', "Î" => 'I', "Ï" => 'I',
        "ì" => 'i', "í" => 'i', "î" => 'i', "ï" => 'i',
        "Ù" => 'U', "Ú" => 'U', "Û" => 'U', "Ü" => 'U',
        "ù" => 'u', "ú" => 'u', "û" => 'u', "ü" => 'u',
        "Ÿ" => 'Y',
        "ÿ" => 'y',
        "Ñ" => 'N',
        "ñ" => 'n'
    );

    $s = preg_replace('`\s+`', '_', strtr($s, $rpl));
    $s = strtolower(preg_replace('`_+`', '_', preg_replace('`[^-_A-Za-z0-9]`', '', $s)));
    return trim($s, '_');
}

答案 1 :(得分:1)

我认为您的代码可以通过以下方式进行压缩:

function urlName($string) {
    $patterns = array('/^[\s-]+|[\s-]+$/', '/[\W]+/');
    $replacements = array('', '-');

    $string = strtr(strtolower($string), 'äöåÄÖÅ', 'aoaaoa');
    // or you can use:
    // $string = strtr(strtolower($string), $someTrMapping);

    return preg_replace($patterns, $replacements, $string);
}