I am trying to create a function which accepts a string and converts it to seolink. But it is looking very un professional
What will be the better way to optimize it? As the may be too many special characters.
The function lookis like
function seo_url($title) {
$titel = substr($title,0,160);
// Replace underscore with "-"
$titel = ereg_replace("_","-",$title);
// Replace space with "-"
$titel = ereg_replace(" ","-",$title);
// Replace special characters
$titel = Ereg_replace ("À", "A", $Titel);
$titel = ereg_replace("í", "i", $title);
$titel = ereg_replace("ó", "o", $title);
$titel = ereg_replace("ú", "u", $title);
$titel = ereg_replace("ñ", "n", $title);
$titel = ereg_replace("Ñ", "n", $title);
$titel = Strtolower (trim($title));
The only real way to make it look neater and smaller would be to use an or expression in your regex.
ie.
ereg_replace("Ñ|ñ", "n", $title);
ereg_replace("è|é|ê|ë","e", $title);
This as far as I have seen is the most common way to do this, and also faster I would hasten to guess.
You can download a character map or create your own for generating the regex string, then store the character map yourself for ease of editing also.
Then your code would be again much smaller and more robust, as it would load in an external character map, which maps each character to it's counterpart.
another solution Removing Accented UTF-8 Characters With PHP
From ways that I know of, I think you'd benefit from reading into .htaccess RewriteRule
could be quite useful from removing this kind of logic from your PHP
Here's an example I found on here that uses this kind of logic (rewriting spaces to dashes for google related SEO)
I use this function to clean urls, is similar to yours but don't use regex:
function seo_url($str) {
$str = mb_strtolower($str);
$str = trim($str);
$str = str_replace(array(' ', '\'', ',', '.', ';', ':'), '', $str);
$str = str_replace('_', '-', $str);
$str = str_replace(array('á', 'é', 'í', 'ó', 'ú', 'ö', 'ü', 'à', 'è', 'ì', 'ò', 'ù', 'â', 'ê', 'î', 'ô', 'û', 'ñ', 'ç'),
array('a', 'e', 'i', 'o', 'u', 'o', 'u', 'a', 'e', 'i', 'o', 'u', 'a', 'e', 'i', 'o', 'u', 'n', 'c'),
$str);
return $str;
}
Use class function Translit::makeUrl($str)
final class Translit {
static function translit($string) {
$replace=array(
"_"=>"-",
"`"=>"",
"a"=>"a",
"À"=>"A",
"б"=>"b",
"Б"=>"B",
// etc.
);
return $str=iconv("UTF-8","UTF-8//IGNORE",strtr($string,$replace));
}
static function makeUrl($str) {
$result = self::translit($str);
$result = str_replace(' ', '-', $result);
$result = preg_replace('/[\-]{2,}/','', $result);
return strtolower($result);
}
}
Why would you say what to replace and NOT what to KEEP in the string. It's like walking backwards. Example:
$string = preg_replace('/[^a-z0-9_]/i', '_', $string);
$string = preg_replace('/_[_]*/i', '_', $string);
and full function below:
public function getStringAsURL($string){
// Define the maximum number of characters allowed as part of the URL
$currentMaximumURLLength = 100;
$string = strtolower($string);
// Any non valid characters will be treated as _, also remove duplicate _
$string = preg_replace('/[^a-z0-9_]/i', '_', $string);
$string = preg_replace('/_[_]*/i', '_', $string);
// Cut at a specified length
if (strlen($string) > $currentMaximumURLLength)
{
$string = substr($string, 0, $currentMaximumURLLength);
}
// Remove beggining and ending signs
$string = preg_replace('/_$/i', '', $string);
$string = preg_replace('/^_/i', '', $string);
return $string;
}