Hey, I'm just wondering if there's any easy function to make text suitable to be in a link, say I have a bunch of caps, weird characters etc and I want it all to be lowercase with "-" instead of spaces, is there a function to do that or do I have to create my own?
+1
A:
Try this one, from snipplr:
function slug($str)
{
$str = strtolower(trim($str));
$str = preg_replace('/[^a-z0-9-]/', '-', $str);
$str = preg_replace('/-+/', "-", $str);
return $str;
}
Skilldrick
2010-04-07 21:23:48
A:
$url = "http://hellO.com/you re here/right.html";
//get rid of spaces
$url = str_replace(" ", "-", $url);
//make lowercase
$url = strtolower($url);
This will give you "http://hello.com/you-re-here/right.html"
Follow this logic for dealing with the weird characters.
Jonathan Mayhak
2010-04-07 21:24:11
A:
You can try preg_replace (http://php.net/manual/en/function.preg-replace.php), which uses regular expressions, combined with strtolower. That makes everything lowercase, then converts anything which is not a lowercase letter (such as a space) into a hyphen.
$str = preg_replace("/[^a-z]/", "-", strtolower($str));
muddybruin
2010-04-07 21:33:22