How do I clear header URIs using PHP?
I am programming a blog and I want the URIs to be a title like the title of a question here on stackoverflow or like wordpress.
What are the rules for sanitizing URIs?
Is there any code already made in PHP that does this?
Thanks in advance,
Omer
+2
the_drow
source
to share
4 answers
Many CMS have implemented something like this, one from Wordpress was posted in another question . You may be wondering about this method too .
+2
soulmerge
source
to share
This might be the shortest way to replace any alphanumeric character with a single hyphen:
trim(preg_replace('/[^a-z0-9-]+/', '-', strtolower($str)), '-')
+6
Gumbo
source
to share
Here's how drupal does it .
In case of site failure:
<?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;
}
+6
raspi
source
to share
Typically, you only want your URL to have 0-9 and az, and make sure everything is lowercase. Replace spaces with dotted lines (-) and split the remainder of the gibberish.
SO pretty much figured it out.
+2
Will morgan
source
to share