HomeCoding & Programming

PHP function to generate SEO-friendly URL titles/slugs

Like Tweet Pin it Share Share Email

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
/**
* 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;

}
?>

Comments (1)

Comments are closed.