How to improve my Seo url generator
i have this function which generates 开发者_开发知识库a seo friendly url from a string :
function seo_titleinurl_generate($title)
{
$title=substr($title,0,160);
$title = ereg_replace(" ", "-", $title); // replace spaces by "-"
$title = ereg_replace("á", "a", $title); // replace special chars
$title = ereg_replace("í", "i", $title); // replace special chars
$title = ereg_replace("ó", "o", $title); // replace special chars
$title = ereg_replace("ú", "u", $title); // replace special chars
$title = ereg_replace("ñ", "n", $title); // replace special chars
$title = ereg_replace("Ñ", "n", $title); // replace special chars
$title = strtolower(trim($title)); // lowercase
$title = preg_replace("/([^a-zA-Z0-9_-])/",'',$title); // only keep standard latin letters and numbers, hyphens and dashes
if($title=="" or $title=="-"){
$mr=rand(1,99999);
$mt=time();
$title=$mr.$mt;
}
return $title;
}
But in some cases when the string has multiple spaces like : the most (3 spaces here)
nice pranks!
it's generates : the-most---nice-pranks
i want it to ignore many spaces and make them only one dash.
Thanks
I think this may be a little faster than the previous answer because it won't mess around with single spaces (I could be wrong):
$title = preg_replace('/\s\s+/', ' ', $title);
Simply add at the beginning:
$title = ereg_replace(/\s+/, " ", $title);
I suggest the following:
/**
* Produce a title with lowercase alphanumeric characters, underscores,
* and dashes. There should be no instances of multiple concurrent dashes,
* and no spaces.
*
* @param string $title the title being sanitized
*
* @return string the sanitized title, or a concatenation of a random
* number and the current time
*/
function seoTitleInUrlGenerate($title)
{
$title = substr(
preg_replace(
array("/([^a-zA-Z0-9_-])/", "/([--]{2,})+/"),
array('', '-'),
strtolower( strtr( trim($title), 'áéíóúñÑ ', 'aeiounN-' ) )
), 0, 160
);
if ($title == "" or $title == "-")
{
return rand(1, 99999) . time();
}
else
{
return $title;
}
}
When tested with the input you provided...
echo seoTitleInUrlGenerate('the most nice pranks!'); // "the-most-nice-pranks"
Rather than returning the random number and time, I would suggest returning FALSE if you were not able to produce a valid title to use in the URL. This way perhaps you can make a record of the invalid title somewhere and fix it later. With the function as it is now, you just get a numerical return value and don't really know if it's the result of an invalid title or if it's a valid title that happened to be full of numbers.
Take a look at the following code:
function Slug($string)
{
return strtolower(trim(preg_replace(array('~[^0-9a-z]~i', '~-+~'), '-', preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8'))), '-'));
}
This uses preg_replace since ereg_replace is deprecated and going away in a future version of PHP. It also uses arrays to reduce the number of function calls and str_replace for one-to-one replacements (it's faster):
function seo_titleinurl_generate($title)
{
$title = substr(strtolower(trim($title)),0,160);
$title = preg_replace('/\s+/', '-', $title); // replace spaces by "-"
$title = str_replace(array("á","í","ó","ú","ñ","Ñ"), array("a","i","o","u","n","n"), $title);// replace special chars
$title = preg_replace('/\W-/', '', $title); // only keep standard latin letters and numbers, hyphens and dashes
if($title=="" or $title=="-"){
$mr=rand(1,99999);
$mt=time();
$title=$mr.$mt;
}
return $title;
}
精彩评论