Create slug with PHP

Free Scripts :: PHP :: String :: Create slug with PHP

Author: Salman Javaid

Website: http://www.salman.be

  • How to create valid slug url with PHP.
  • Create SEO Friendly url with custom PHP function.

Function accepts a string parameters and returns valid url. It removes all charachers from the string which are not supported by url.

Function

function generateSlug($phrase) {
    $result = strtolower($phrase);
    $result = str_replace(array('/', '-', '+', '.', '&', '&'), ' ', $result);
    $result = str_replace(array('ë','é', 'è'), 'e', $result);
    $result = preg_replace("/[^a-z0-9s-]/", "", $result);
    $result = trim(preg_replace("/s+/", " ", $result));
    $result = trim(substr($result, 0, 45));
    $result = preg_replace("/s/", "-", $result);
    return $result;
}

Calling The Function:

$phrase = "php/mysql tutorial";
echo generateSlug($phrase);

Output:

php-mysql-tutorial

Comments

Reboot - June 22, 2010
Verry usefull tutorial. Helped me alot!

Leave a comment

Full Name*
Comments*
Security Code*

Our Partners

Getty Icons  Free MP3 Songs
Free High Quality Images  Learn Free Languages 
Opal Hosting  R-Tech Solutions
Virtualxone
  • Submit a Script

    Click here to submit your script.