Generating Search Engine Friendly Titles for your URL
Tuesday, July 8th, 2008 at 6:42am, in String Manipulation, written by Stefan Ashwell
For most dynamic websites you will want to create "Search Engine Friendly" or "User Friendly" URLs, rather than using ugly and meaningless strings or IDs to reference your content. The way you interpret the URLs and use them to get your content is for another article and I won't go into that right now, what I want to talk about is how to turn your page titles into versions that can be used in your URL.
For example you have a page called "Search Engine Friendly URLs". In the URL you will ideally want to have this string, only lowercase and with dashes in place of the spaces: "search-engine-friendly-urls". Therefore the URL to the page becomes "http://www.yourdomain.com/search-engine-friendly-urls/" or similar.
To do this we will write a PHP function that will return the nicely formatted Search Engine Friendly URL.
function sef_url($string) {
}
As well as turning the string lowercase and replacing spaces with dashes, it is also an idea to replace any characters we don't want with a dash too. So to start with we'll create an "allowed list" of characters, any others will be replaced with dashes. We'll also make the string all lower case at this point with PHP's strtolower function.
function sef_url($string) {
$allow = 'abcdefghijklmnopqrstuvwxyz1324567890-';
$string = strtolower(string);
}
In order to check each letter against our allowed list, we need to turn the allowed list into an array of letters that we can check against. This is relatively simple with PHP:
function sef_url($string) {
$allowed = 'abcdefghijklmnopqrstuvwxyz1324567890-';
$string = strtolower(string);
$temp = array();
for($x = 0; $x < strlen($allowed); $x++) {
$temp[] = $allowed[$x];
}
$allowed = $temp;
}
Now we have our allowed list as an array we can use PHP's in_array function to see if all of the characters in our string match one in the allowed list. If they don't replace them with a dash.
function sef_url($string) {
$allowed = 'abcdefghijklmnopqrstuvwxyz1324567890-';
$string = strtolower(string);
$temp = array();
for($x = 0; $x < strlen($allowed); $x++) {
$temp[] = $allowed[$x];
}
$allowed = $temp;
$result = '';
for($x = 0; $x < strlen($string); $x++) {
if(in_array($string[$x], $allowed) > -1) {
$result .= $string[$x];
} else {
$result .= '-';
}
}
return $result;
}
We now have a function that turns a string into one that is lowercase and has dashes in place of any spaces or special characters. An optional extra you can include in this function is something to help stop multiple dashes in a row appearing. For example you may have a title with an exclamation mark and then a space - this will result in two dashes next to each other. To neaten this up you can replace multiple dashes with a single one like so:
$result = str_replace('--', '-', $result);
$result = str_replace('--', '-', $result);
$result = str_replace('--', '-', $result);
Doing this a few times in a row will remove duplicates quite nicely.
Now all you need to do is call this function when you want to turn something into a Search Engine Friendly URL string. This could be in your CMS or on user submission etc.
$url = sef_url($_POST['title']);
Dashes or Underscores?
There is a discussion as to whether dashes or underscores are better in URLs. Personally I don't think it makes that much difference, if any at all. I like to use dashes in mine purely because I think it looks neater. Some people like to use underscores, and if you wish to do so you can simply replace the dashes in the function with underscores and it will work quite happily.
I hope you found this tutorial helpful. If you have any comments or ideas about Search Engine Friendly URLs please don't hesitate to leave a comment.

















Comments on this article
By otavio, Tuesday, July 8th, 2008 at 11:38am
I use that function .. to ISO-8859-1
function monta_url( $x )
{
$x = trim($x);
$x = html_entity_decode( $x);
$r = array( '/[ÂÀÁÄÃ]/'=>'A','/[âãàáä]/'=>'a','/[ÊÈÉË]/' =>'E','/[êèéë]/' =>'e','/[ÎÍÌÏ]/' =>'I','/[îíìï]/' =>'i','/[ÔÕÒÓÖ]/'=>'O','/[ôõòóö]/'=>'o','/[ÛÙÚÜ]/' =>'U','/[ûúùü]/' =>'u','/ç/' =>'c','/Ç/' => 'C');
$x = preg_replace( array_keys( $r ), array_values( $r ), $x);
$x = preg_replace( '/\s|\'|\"/', '-', $x);
preg_match_all('/[a-z]|\-[a-z]|[0-9]/i',$x, $R);
$x = implode('',$R[0]);
$x = strtolower( $x);
$x = print_r( $x, true);
return $x;
}
Please log in to post a comment about this article.