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:
A slightly cleaner way of doing this in PHP at least is:
function CleanForUrl($urlPart, $maxLength = null) {
$url = strtolower(preg_replace(array('/[^a-z0-9\- ]/i', '/[ \-]+/'), array('', '-'), trim($urlPart)));
if ($maxLength) $url = substr($url, 0, $maxLength);
return $url;
}
Might as well do the trim()
at the start so there is less to process later and the full replacement is done with in the preg_replace()
.
Thxs to cg for coming up with most of this: What is the best way to clean a string for placement in a URL, like the question name on SO?