Tags
PHP
Asked 8 years ago
10 Oct 2016
Views 1282
debugger

debugger posted

String to Seo Friendly Url in php

i want to make SEO friendly url so page title also include in the page link instead of just blog post ID
Suppose
i have Page Title like
Shakespeare invented the words ‘assassination’ and ‘bump’ and have Post ID :12
so instead of link http://www.fact-demon.com/index.php?post-id=12
i want link like http://www.fact-demon.com/Shakespeare-invented-the-words-assassination-and-bump/12
for Url making from String i made following code

function SeoUrl($pagetitle){
return str_replace(" ","-",$pagetitle);
}


it generate link ::
http://www.factdemon.com/Shakespeare-invented-the-words-%E2%80%98assassination%E2%80%99-and-%E2%80%98bump%E2%80%99

and by browsing it
i got error

Not found Error
Shakespeare-invented-the-words-‘assassination’-and-‘bump’ was not found on this server.


so i need proper function for String to Seo Friendly Url
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00


function SeoUrl($string)
{
 		$tempUrl = str_replace('-', ' ', $string);
  		$tempUrl = trim(strtolower($tempUrl));
 		$tempUrl = preg_replace('/(\s|[^A-Za-z0-9\-])+/', '-', $tempUrl);
 		$tempUrl = trim($tempUrl, '-');

		return $tempUrl;
}


above function will
Remove any '-' from the string since they will be used as concatenates ,
Trim white spaces at beginning and end of alias and make lowercase ,
Remove any duplicate whitespace, and ensure all characters are alphanumeric,
Trim dashes at beginning and end of alias

in short it make your URL safe for browsing and Seo Friendly
Post Answer