Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过替换特殊字符实现seolink生成器功能_Php_Cakephp - Fatal编程技术网

Php 通过替换特殊字符实现seolink生成器功能

Php 通过替换特殊字符实现seolink生成器功能,php,cakephp,Php,Cakephp,我试图创建一个函数,它接受字符串并将其转换为seolink。 但它看起来很不专业 优化它的更好方法是什么?因为可能有太多的特殊字符 函数看起来像 function seo_url($title) { $titel = substr($title,0,160); // Replace underscore with "-" $titel = ereg_replace("_","-",$title); // Replace s

我试图创建一个函数,它接受字符串并将其转换为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)); 

据我所知,我认为你会从阅读

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


我在这里发现了一个使用这种逻辑(将空格改写为破折号,用于谷歌相关的搜索引擎优化)

让它看起来更整洁、更小的唯一真正方法是在正则表达式中使用or表达式

据我所知,这是最常见的方法,而且我会更快地猜测

您可以下载字符映射或创建自己的用于生成正则表达式字符串的字符映射,然后自己存储字符映射以便于编辑

然后,您的代码将再次变得更小、更健壮,因为它将加载到外部角色映射中,该映射将每个角色映射到对应的角色

另一种解决方案

我使用此函数清理URL,与您的类似,但不使用正则表达式:

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;
}

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


你为什么要说替换什么而不是保留什么。这就像向后走。例如:

$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; 
        } 

对于这样一个简单的函数,即使有两个方法,你也不需要一个静态类!我只是从我的工作项目中得到它,在那里我单独使用ever函数。
$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; 
        }