Take a string such as:
In C#: How do I add \"Quotes\" around string in a comma delimited list of strings?
and convert it to:
I would follow these steps:
preg_replace()
function call already prevents multiple hyphens)So, all together in a function (PHP):
function generateUrlSlug($string, $maxlen=0)
{
$string = trim(preg_replace('/[^a-z0-9]+/', '-', strtolower($string)), '-');
if ($maxlen && strlen($string) > $maxlen) {
$string = substr($string, 0, $maxlen);
$pos = strrpos($string, '-');
if ($pos > 0) {
$string = substr($string, 0, $pos);
}
}
return $string;
}