I would like to sanitize a string in to a URL so this is what I basically need:
- Everything must be removed except alphanumeric characters and spaces and dashed.
- Spaces should be converter into dashes.
Eg.
JavaScript
x
This, is the URL!
must return
JavaScript
this-is-the-url
Advertisement
Answer
JavaScript
function slug($z){
$z = strtolower($z);
$z = preg_replace('/[^a-z0-9 -]+/', '', $z);
$z = str_replace(' ', '-', $z);
return trim($z, '-');
}