I'm looking to create a URL string like the one SO uses for the links to the questions. I am not looking at rewriting the url (mod_rewrite). I am looking at generating the link on the page.
Example: The question name is:
Is it better to use ob_get_contents() or $text .= ‘test’;
The URL ends up being:
http://stackoverflow.com/questions/292068/is-it-better-to-use-obgetcontents-or-text-test
The part I'm interested in is:
is-it-better-to-use-obgetcontents-or-text-test
So basically I'm looking to clean out anything that is not alphanumeric while still keeping the URL readable. I have the following created, but I'm not sure if it's the best way or if it covers all the possibilities:
$str = urlencode(
strtolower(
str_replace('--', '-',
preg_replace(array('/[^a-z0-9 ]/i', '/[^a-z0-9]/i'), array('', '-'),
trim($urlPart)))));
So basically:
- trim
- replace any non alphanumeric plus the space with nothing
- then replace everything not alphanumeric with a dash
- replace -- with -.
strtolower()
urlencode()
-- probably not needed, but just for good measure.