Slug URL generation function overriding the Ç

2019-09-16 10:14发布

问题:

I have this function above to create url slugs from posts title, the problem is that the ç characther is not being converted to c. It is actually being override by the function.

Example post title: Coração de Pelúcia

The slug generated: coraao-de-pelucia

How can i fix this function to generate the slug like: coracao-de-pelucia

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

回答1:

You should use the iconv module and a function such as this one to do the conversion:

function url_safe($string){
    $url = $string;
    setlocale(LC_ALL, 'pt_BR'); // change to the one of your language
    $url = iconv("UTF-8", "ASCII//TRANSLIT", $url);  
    $url = preg_replace('~[^\\pL0-9_]+~u', '-', $url);
    $url = trim($url, "-");
    $url = strtolower($url);
    return $url;
}