2

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

asked Sep 16, 2009 at 11:43

4 Answers 4

6

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

trim(preg_replace('/[^a-z0-9-]+/', '-', strtolower($str)), '-')
answered Sep 16, 2009 at 11:59
Sign up to request clarification or add additional context in comments.

Comments

6

Here's how drupal does it.

In case of site goes down:

<?php
function pathauto_cleanstring($string)
{
 $url = $string;
 $url = preg_replace('~[^\\pL0-9_]+~u', '-', $url); // substitutes anything but letters, numbers and '_' with separator
 $url = trim($url, "-");
 $url = iconv("utf-8", "us-ascii//TRANSLIT", $url); // TRANSLIT does the whole job
 $url = strtolower($url);
 $url = preg_replace('~[^-a-z0-9_]+~', '', $url); // keep only letters, numbers, '_' and separator
 return $url;
}
answered Sep 16, 2009 at 15:57

1 Comment

You might want to post a full answer rather than just a link in case the linked page goes down.
2

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.

answered Sep 16, 2009 at 11:50

Comments

2

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.

answered Sep 16, 2009 at 11:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.