Tejas Rana

SEO friendly URL generator

Hi, here is a function which I create for generating SEO friendly URL.
first we have to create a function called “friendlyURL”.
here is a code

<?php
function friendlyURL($inputString){
$url = strtolower($inputString);
$patterns = $replacements = array();
$patterns[0] = '/(&amp;|&)/i';
$replacements[0] = '-and-';
$patterns[1] = '/[^a-zA-Z01-9]/i';
$replacements[1] = '-';
$patterns[2] = '/(-+)/i';
$replacements[2] = '-';
$patterns[3] = '/(-$|^-)/i';
$replacements[3] = '';
$url = preg_replace($patterns, $replacements, $url);
return $url;
}
?>

 
Now this function will convert string into a SEO friendly URL.
How to use it?

<?php
echo friendlyURL('hi this is a seo friendly url & it convert/replace special characters.');
?>

 

Exit mobile version