0

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.

b4hand
  • 9,550
  • 4
  • 44
  • 49
CHAVOUSA
  • 1
  • 1

2 Answers2

0

There are a couple of good ways in this SO question: Convert accented characters to their plain ascii equivalents

iconv may work, though I think for your application the top answer may be the best: it has a link to the comments of strtr with an array of accents to be replaced by their ascii equivalents

Community
  • 1
  • 1
Jonathan
  • 1,542
  • 3
  • 16
  • 24
  • thank you for your reply. As stated in the details of my question, I had already found several functions to convert accented characters to english characters. However, the problem I was having was that I was using two different functions in my script, one to convert text fields to SEO links and the other to replace the characters. For some reason I could not make them to work together. – CHAVOUSA Dec 01 '14 at 06:20
0

After spending a few more hours playing around with the two scripts, I came up with the solution that did both, convert text field to SEO links and change the accented characters.

The issue that I was having was the order of the functions. By using the function to convert characters first, the SEO function follows with the correct conversion to SEO links using all english characters.

The code I used was generate_seo_link(convertAccented($script)); and the two funtions were saved in a separate linked page as shown below:

/* REPLACE ACCENTED LETTERS */
function convertAccented($str)
{   $accented = array("á", "é", "ó", "ú", "ñ","í");
     $cleanlink = array("a", "e", "o", "u", "n","i");
     return str_replace($accented, $cleanlink, $str);
}    

*/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);
}
CHAVOUSA
  • 1
  • 1