我试图从专有名称生成适当的链接......
例如: T& J Automotive 目前生成 / t-j-automotive
但是,因为我需要根据名称进行查找,所以在尝试转换回名称时我无法执行查找。
所以...我通过将它们转换为_来照顾它,这对于Mike's Shop(转换为mike_s-shop)这样的名字非常有用,但现在我面对的是&
这是我目前的职能:
// Fix the name for a SEO friendly URL
function FixNameForLink($str){
// Swap out Non "Letters" with a -
$text = preg_replace('/[^\\pL\d\']+/u', '-', $str);
// Trim out extra -'s
$text = trim($text, '-');
// Convert letters that we have left to the closest ASCII representation
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
// Make text lowercase
$text = strtolower($text);
// ' has been valid until now... swap it for an _
$text = str_replace('\'', '_', $text);
// & has been valid until now... swap it for an .
$text = str_replace('&', '.', $text);
// Strip out anything we haven't been able to convert
$text = preg_replace('/[^-_\w]+/', '', $text);
return $text;
}
注意,&更换不会发生。如何确保传递给此函数的任何字符串都将'替换为_,和&换成。?
答案 0 :(得分:0)
修正:
// Fix the name for a SEO friendly URL
function FixNameForLink($str){
// Swap out Non "Letters" with a -
$text = preg_replace('/[^\\pL\d\'&]+/u', '-', $str); // needed to allow the &
// Trim out extra -'s
$text = trim($text, '-');
// Convert letters that we have left to the closest ASCII representation
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
// Make text lowercase
$text = strtolower($text);
// ' has been valid until now... swap it for an _
$text = str_replace('\'', '_', $text);
// & has been valid until now... swap it for an .
$text = str_replace('&', '.', $text);
// Strip out anything we haven't been able to convert
$text = preg_replace('/[^-_\.\w]+/', '', $text); // needed to make sure the replace . stays put
return $text;
}