0

我正在尝试创建一个接受字符串并将其转换为 seolink 的函数。但它看起来很不专业

优化它的更好方法是什么?因为可能有太多特殊字符。

函数看起来像

  function seo_url($title) {
        $titel = substr($title,0,160);

        // Replace underscore with "-" 
        $titel = ereg_replace("_","-",$title);

        // Replace space with "-" 
        $titel = ereg_replace(" ","-",$title); 

       // Replace special characters
        $titel = Ereg_replace ("À", "A", $Titel); 
        $titel = ereg_replace("í", "i", $title); 
        $titel = ereg_replace("ó", "o", $title); 
        $titel = ereg_replace("ú", "u", $title); 
        $titel = ereg_replace("ñ", "n", $title); 
        $titel = ereg_replace("Ñ", "n", $title); 

        $titel = Strtolower (trim($title)); 
4

5 回答 5

1

您为什么要在字符串中说要替换的内容而不是要保留的内容。这就像倒退着走。例子:

$string = preg_replace('/[^a-z0-9_]/i', '_', $string); 
$string = preg_replace('/_[_]*/i', '_', $string); 

和下面的完整功能:

public function getStringAsURL($string){ 
            // Define the maximum number of characters allowed as part of the URL 

            $currentMaximumURLLength = 100; 

            $string = strtolower($string); 

            // Any non valid characters will be treated as _, also remove duplicate _ 

            $string = preg_replace('/[^a-z0-9_]/i', '_', $string); 
            $string = preg_replace('/_[_]*/i', '_', $string); 

            // Cut at a specified length 

            if (strlen($string) > $currentMaximumURLLength) 
            { 
                $string = substr($string, 0, $currentMaximumURLLength); 
            } 

            // Remove beggining and ending signs 

            $string = preg_replace('/_$/i', '', $string); 
            $string = preg_replace('/^_/i', '', $string); 

            return $string; 
        } 
于 2012-10-08T11:02:09.860 回答
1

我使用此功能来清理网址,与您的类似,但不使用正则表达式:

function seo_url($str) {
  $str = mb_strtolower($str);
  $str = trim($str);
  $str = str_replace(array(' ', '\'', ',', '.', ';', ':'), '', $str);
  $str = str_replace('_', '-', $str);
  $str = str_replace(array('á', 'é', 'í', 'ó', 'ú', 'ö', 'ü', 'à', 'è', 'ì', 'ò', 'ù', 'â', 'ê', 'î', 'ô', 'û', 'ñ', 'ç'),
                     array('a', 'e', 'i', 'o', 'u', 'o', 'u', 'a', 'e', 'i', 'o', 'u', 'a', 'e', 'i', 'o', 'u', 'n', 'c'),
                     $str);


  return $str;
}
于 2012-10-01T13:42:52.840 回答
0

The only real way to make it look neater and smaller would be to use an or expression in your regex.

ie.

ereg_replace("Ñ|ñ", "n", $title);
ereg_replace("è|é|ê|ë","e", $title);

This as far as I have seen is the most common way to do this, and also faster I would hasten to guess.

You can download a character map or create your own for generating the regex string, then store the character map yourself for ease of editing also.

Then your code would be again much smaller and more robust, as it would load in an external character map, which maps each character to it's counterpart.

another solution Removing Accented UTF-8 Characters With PHP

于 2012-10-01T13:30:17.800 回答
0

据我所知,我认为你会从阅读.htaccess RewriteRule中受益

从 PHP 中删除这种逻辑可能非常有用

这是我在这里找到的一个使用这种逻辑的示例(将空格重写为与谷歌相关的 SEO 的破折号)

于 2012-10-01T13:27:09.453 回答
0

使用类函数Translit::makeUrl($str)

final class Translit {

    static function translit($string) {

        $replace=array(
            "_"=>"-",
            "`"=>"",
            "a"=>"a",
            "À"=>"A",
            "б"=>"b",
            "Б"=>"B",
            // etc.
        );
        return $str=iconv("UTF-8","UTF-8//IGNORE",strtr($string,$replace));
    }

    static function makeUrl($str) {
        $result = self::translit($str);

        $result = str_replace(' ', '-', $result);
        $result = preg_replace('/[\-]{2,}/','', $result);

        return strtolower($result);
    }
}
于 2012-10-01T13:45:20.930 回答