Online Snippets Library

Create Seo friendly URLs From raw String In PHP

Some times We need to create URL From raw content String PHP.Seo friendly URLs improves site search engines ranking.Here we explains how to create SEO friendly URL in PHP.

Example

<?php
 function seoFriendlyUrl($string,$length)
   {
    $string=trim($string, "."); // Remove "." from string
    $string=explode("\n", wordwrap($string,$length, "\n"));  // Restrict String Length
    $string= $string[0];
    $string = str_replace(array('[\', \']'), '', $string);
    $string = preg_replace('/\[.*\]/U', '', $string);
    $string = preg_replace('/&(amp;)?#?[a-z0-9]+;/i', '-', $string);
    $string = htmlentities($string, ENT_COMPAT, 'utf-8');
    $string = preg_replace('/&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);/i', '\\1', $string );
    $string = preg_replace(array('/[^a-z0-9]/i', '/[-]+/') , '-', $string);
    return strtolower(trim($string, '-'));
       
   }
echo $result=seoFriendlyUrl('Some times We need to create URL From raw content String PHP.Seo friendly URLs improves site search engines ranking.','20');

?>

Out Put

some-times-we-need