欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

PHP 生成友好URL 【用于SEO】

程序员文章站 2022-05-19 20:49:47
...

[PHP]代码

 public function friendlyURL($string, $replacement = '-') {
        $map = array(
            '/à|á|å|â|ä/' => 'a',
            '/è|é|ê|ẽ|ë/' => 'e',
            '/ì|í|î/' => 'i',
            '/ò|ó|ô|ø/' => 'o',
            '/ù|ú|ů|û/' => 'u',
            '/ç|č/' => 'c',
            '/ñ|ň/' => 'n',
            '/ľ/' => 'l',
            '/ý/' => 'y',
            '/ť/' => 't',
            '/ž/' => 'z',
            '/š/' => 's',
            '/æ/' => 'ae',
            '/ö/' => 'oe',
            '/ü/' => 'ue',
            '/Ä/' => 'Ae',
            '/Ü/' => 'Ue',
            '/Ö/' => 'Oe',
            '/ß/' => 'ss',
        	'/ /'=>' ',
        	'/ /'=>'',
        	'/~|·|!|@|#|¥|%|…|&|×|(|)|-|\+|=|『|【|』|】|、|:|;|“|”|’|《|,|》|。|?|\/|—|_|‘|:|√|<|°|丶/'=>' ',
         	'/[^\w\s\x80-\xff]/' => ' ',
            '/\\s+/' => $replacement
        );

        $string = preg_replace(array_keys($map), array_values($map), $string);
       	$string = preg_replace('/\\s+/',$replacement, strtolower($string));
       	$string = trim($string,$replacement);
        return $string;
    }
相关标签: php