views:

254

answers:

4

Hello,
I am programming a blog and I want the URIs to be the title like the question title here in stackoverflow or like wordpress.
What are the rules for sanitizing a URI?
Is there an already made code in PHP that does this?

Thanks in advance,
Omer

+2  A: 

Many CMS's have implemented something like that, the one of Wordpress has been posted in another question. You might be interested in the question about this technique in general, too.

soulmerge
+2  A: 

Generally you'll want your URL to have only 0-9 and a-z, and make sure that everything is lowercase. Replace spaces with dashes (-), and strip the rest of the gibberish.

SO pretty much has it figured out.

Will Morgan
+2  A: 

This might be the shortest way to replace any non alphanumeric character with a single hyphen:

trim(preg_replace('/[^a-z0-9-]+/', '-', strtolower($str)), '-')
Gumbo
+2  A: 

Here's how drupal does it.

raspi