子弹功能删除带有URL重音的字母

时间:2019-02-20 01:38:19

标签: php slug

下面的功能可以正常工作,但是我不能理解排除特殊字符的切换原因:

   function slug( $string, $separator = '-' ) {
        $accents_regex = '~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i';
        $special_cases = array( '&' => 'and', "'" => '');
        $string = mb_strtolower( trim( $string ), 'UTF-8' );
        $string = str_replace( array_keys($special_cases), array_values( $special_cases), $string );
        $string = preg_replace( $accents_regex, '$1', htmlentities( $string, ENT_QUOTES, 'UTF-8' ) );
        $string = preg_replace("/\s+/", "$separator", $string);
        $string = preg_replace("/[$separator]+/u", "$separator", $string);
        return $string;
    }

示例:

字符串:"um rádio bonito"

结果应为:"um-radio-bonito"

但结果是:"um-rdio-bonito"

1 个答案:

答案 0 :(得分:1)

此功能可以为您提供帮助

function cleanAccents($r)
{
    $r = preg_replace("/ß/","ss", $r);
    $r = preg_replace("/[àáâãäå]/","a", $r);
    $r = preg_replace("/æ/","ae", $r);
    $r = preg_replace("/ç/","c", $r);
    $r = preg_replace("/[èéêë]/","e", $r);
    $r = preg_replace("/[ìíîï]/","i", $r);
    $r = preg_replace("/ñ/","n", $r);
    $r = preg_replace("/[òóôõö]/","o", $r);
    $r = preg_replace("/œ/","oe", $r);
    $r = preg_replace("/[ùúûü]/","u", $r);
    $r = preg_replace("/[ýÿ]/","y", $r);        
    return $r;
}