替换重音字母作为PHP中现有SEO功能的一部分


Replace accented letters as part of existing SEO function in PHP

我发现了以下函数,可以从我的文章和类别的名称中创建SEO链接。我遇到的问题是西班牙语版本,因为它们有重音字母。

该功能与英语完美配合,但是当我切换到西班牙语时,该功能会从 SEO 链接中删除所有重音字母。我想做的是用它们的非重音版本替换它们

    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);
}

然后,我尝试创建一个单独的函数来替换我计划与上述函数一起使用的重音字母。

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

但是,当我尝试组合这两个功能时,重音字母仍然被删除,而不是被非重音字母替换。

我已经试过了 convertAccented(generate_seo_link($categoryname));generate_seo_link(convertAccented($categoryname));没有成功...

我还尝试在第一个函数中将strtolower($input)替换为mb_strtolower($input, 'UTF-8'),正如另一个问答中所建议的那样,仍然没有成功。

我需要帮助来确定如何将重音字母

的变化从上面的第一个函数合并到非重音字母。

例如,类别"Tarjetas de Crédito"将转换为"tarjetas-de-crdito",而不带最后一个"e"。而不是"tarjetas-de-credito">

我已经浏览了该网站并发现了一堆相关问题,但它们并不能完全回答我的问题。

在这个SO问题中有几个好方法: 将重音字符转换为普通的 ascii 等效字符

iconv 可能会起作用,尽管我认为对于您的应用程序来说,最高答案可能是最好的:它有一个指向 strtr 注释的链接,其中包含一系列重音,可替换为其 ASCII 等效项

在花了几个小时玩弄这两个脚本之后,我想出了同时完成这两个操作的解决方案,将文本字段转换为 SEO 链接并更改重音字符。

我遇到的问题是函数的顺序。通过使用函数首先转换字符,SEO函数随后使用所有英文字符正确转换为SEO链接。

我使用的代码generate_seo_link(convertAccented($script));,两个功能保存在一个单独的链接页面中,如下所示:

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