PHP 从给定的字符串生成slug或永久链接
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 从给定的字符串生成slug或永久链接相关的知识,希望对你有一定的参考价值。
/**
* Rewrite strings to be URL/SEO friendly
* @param string $text
* @return string
*/
function slugify($text) {
// Map thanks to CakePHP
$map = array(
'/à|á|å|â/' => 'a',
'/è|é|ê|?|ë/' => 'e',
'/ì|ÃÂ|î/' => 'i',
'/ò|ó|ô|ø/' => 'o',
'/ù|ú|?|û/' => 'u',
'/ç/' => 'c',
'/ñ/' => 'n',
'/ä|æ/' => 'ae',
'/ö/' => 'oe',
'/ü/' => 'ue',
'/�/' => 'Ae',
'/�/' => 'Ue',
'/�/' => 'Oe',
'/�/' => 'ss',
'/[^\w\s]/' => ' ',
);
$text = preg_replace(array_keys($map), array_values($map), $text);
$text = preg_replace('/[^-a-zA-Z0-9&\s]/i', '', $text);
$text = trim(strtolower($text));
$text = str_replace(array('-', ' ', '&'), array('_', '-', 'and'), $text);
$text = urlencode($text);
return $text;
}
以上是关于PHP 从给定的字符串生成slug或永久链接的主要内容,如果未能解决你的问题,请参考以下文章