Create SEO Friendly Page or Slug URL using PHP

Tuesday, August 23, 2011

Creating Search Engine 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 Search Engine Friendly SEO Friendly Page URL from given string. while you are generating Dynamic content.


<?php

$PageTitle="<h1>Education 's and Cultural <pre>Programs\" for <br> schools! and colleges!  (2010-11)</h1>";

// @ Desc: Prepare SEO Friendly URL 
// @ Parm: String with Mixed with tags and special characters 
// @ Return: SEO Frinedly URL out by replacing spaces and special characters with Hyphen(-)
function prepare_seo_friendly_url($string)
{
	// Remove HTML Tags if found
	$string=strip_tags($string);
	// Replace special characters with white space
	$string=preg_replace('/[^A-Za-z0-9-]+/', ' ', $string);
	// Trim White Spaces and both sides
	$string=trim($string);
	// Replace whitespaces with Hyphen (-) 
	$string=preg_replace('/[^A-Za-z0-9-]+/','-', $string);	
	// Conver final string to lowercase
	$slug=strtolower($string);
	return $slug;
}

// Example Output:
echo prepare_seo_friendly_url($PageTitle);

// Output:
// education-and-cultural-programs-for-schools-and-colleges-2010-11
?>

No comments:

Post a Comment