Clean URL

Cleans a URL string, for SEO friendly links

<?php

function cleanURL($string)
{
    $url = str_replace("'", '', $string);
    $url = str_replace('%20', ' ', $url);
    $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);  // you may opt for your own custom character map for encoding.
    $url = strtolower($url);
    $url = preg_replace('~[^-a-z0-9_]+~', '', $url); // keep only letters, numbers, '_' and separator
    return $url;
}

// echo cleanURL("Shelly's%20Greatest%20Poem%20(2008)");  // shellys-greatest-poem-2008

?>

Usage

Useful for blog posts for instance, where a title can be something like

"It's over 100 degrees today!"

which would translate to

"its-over-100-degrees-today". So you can store the string and call it like http://examplesite.com/news/2008/its-over-100-degrees-today


Comments

guest
Posted on 27.08.2012 08:35

very nice...

guest
Posted on 20.10.2011 19:23

Thank you so much !! :)

guest
Posted on 07.10.2011 13:23

thanks...........dear.

guest
Posted on 19.07.2011 14:22

Thanks for helpful function :)

guest
Posted on 28.10.2010 05:34

thanks my friend.

guest
Posted on 13.09.2010 02:12

$url = preg_replace('~[^\\pL0-9_]+~u', '-', $url); // substitutes anything but letters, numbers and '_' with separator

That line removed all LETTERS from my links... which aint good, i like letters.

guest
Posted on 19.07.2010 06:56

Thanks for that. It is very useful..

guest
Posted on 15.02.2010 22:07

thank u m8...

guest
Posted on 09.02.2010 04:45

Sexy Script dear..... I love it...

guest
Posted on 13.12.2008 14:45

hi myfriends
I want to use this function . but I downt now how?
please show me that where i place this variable $URL.

thanks.

Add your comment