Removing the -
from the list of accepted characters will yield the desired result:
$slug = preg_replace('/[^A-Za-z0-9]+/', '-', $s);
Returned value: This-is-a-slug
题
I am creating search engine friendly slug. Which works fine,but one issue is in it. My code is given below
$s = "This is a - slug";
function slug($s){
$slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $s);
return $slug;
}
This code result in this string This-is-a---slug. But I require This-is-a-slug. Is there any way that when - come with string,then it adjusts correctly.
解决方案
Removing the -
from the list of accepted characters will yield the desired result:
$slug = preg_replace('/[^A-Za-z0-9]+/', '-', $s);
Returned value: This-is-a-slug