I found the following function to create SEO links out of the names of my articles and categories. The problem that I am having is with the Spanish versions since they have accented letters.
The function works perfectly with English, but when I switch to Spanish the function removes all accented letters from the SEO links. What I want to do instead is to replace them by their non-accent versions
GENERATE SEO URLS */
function generate_seo_link($input,$replace = '-',$remove_words = true,$words_array = array())
{
//make it lowercase, remove punctuation, remove multiple/leading/ending spaces
$return = trim(ereg_replace(' +',' ',preg_replace('/[^a-zA-Z0-9\s]/','',strtolower($input))));
//remove words, if not helpful to seo
//i like my defaults list in remove_words(), so I wont pass that array
if($remove_words) { $return = remove_words($return,$replace,$words_array); }
//convert the spaces to whatever the user wants
//usually a dash or underscore..
//...then return the value.
return str_replace(' ',$replace,$return);
}
/* takes an input, scrubs unnecessary words */
function remove_words($input,$replace,$words_array = array(),$unique_words = true)
{
//separate all words based on spaces
$input_array = explode(' ',$input);
//create the return array
$return = array();
//loops through words, remove bad words, keep good ones
foreach($input_array as $word)
{
//if it's a word we should add...
if(!in_array($word,$words_array) && ($unique_words ? !in_array($word,$return) : true))
{
$return[] = $word;
}
}
//return good words separated by dashes
return implode($replace,$return);
}
I then tried creating a separate function to replace the accent letters which I planned to use together with the function above.
function convertAccented($str)
{ $accented = array("á", "é", "ó", "ú", "ñ","í");
$cleanlink = array("a", "e", "o", "u", "n","i");
return str_replace($accented, $cleanlink, $str);
}
However, when I try to combine both functions, the accented letters are still being removed instead of replaced by their non-accent counterparts.
I already tried
convertAccented(generate_seo_link($categoryname));
and
generate_seo_link(convertAccented($categoryname));
without success...
I also tried replacing strtolower($input)
to mb_strtolower($input, 'UTF-8')
in the first function, as suggested in another Q&A, still no success.
I need help in determining how to incorporate the change of accent to non-accent letters from the first function above.
For example, the category "Tarjetas de Crédito" will be converted to "tarjetas-de-crdito", without the last "e". Instead of "tarjetas-de-credito"
I already browsed the site and found a bunch of related questions, but they do not quite answer my question.