PHP function to generate SEO-friendly URL titles/slugs

Creating SEO-Friendly page URL from your Page Title / heading will very helpful to generate a highly optimized keyword rich URLs for your website, Here is the function to get the SEO-Friendly Page URL from given string.

[php]
<?php
/**
* Return URL-Friendly string slug
* @param string $string
* @return string
*/
function seoUrl($string) {

//Unwanted: {UPPERCASE} ; / ? : @ & = + $ , . ! ~ * ‘ ( )
$string = strtolower($string);

//Strip any unwanted characters
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string);

//Clean multiple dashes or white spaces
$string = preg_replace("/[\s-]+/", " ", $string);

//Convert whitespaces and underscore to dash
$string = preg_replace("/[\s_]/", "-", $string);
return $string;

}
?>
[/php]

One thought on “PHP function to generate SEO-friendly URL titles/slugs”

Comments are closed.