Share
ShareSidebar
2011-09-25 22:39:26 +0

Friendly URLs In PHP - Function

The purpose of this role is take a string and turn it into a friendly string to search engines.

function friendly_url($url) {
    $url
= strtolower($url);
    $find
= array('á', 'é', 'í', 'ó', 'ú', 'ñ');
    $repl
= array('a', 'e', 'i', 'o', 'u', 'n');
    $url
= str_replace($find, $repl, $url);
    $find
= array(' ', '&', '\r\n', '\n', '+', '/');
    $url
= str_replace($find, '-', $url);
    $find
= array('/[^a-z0-9\-<>]/', '/[\-]+/', '/<[^>]*>/');
    $repl
= array('', '-', '');
    $url
= preg_replace($find, $repl, $url);
   
return $url;
}

$url
= "this is a test";
echo
"http://www.mysite.com/".friendly_url($url); //"http://www.mysite.com/this-is-a-test"

Rate this post

You must be registered to vote first

Comments

Comment