Create slug with PHP
Free Scripts :: PHP :: String :: Create slug with PHP
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





