2016-07-24 48 views
2

enter image description herePHP函數創建永久像WordPress

您好我想PHP函數來創建永久一樣,支持Unicode字符也是WordPress的。目前我使用下面的功能工作良好,但不支持Unicode字符。

function create_slug_html($string, $ext='.html'){  
    $replace = '-';   
    $string=strtolower($string);  
    $string=trim($string); 
    //remove query string  
    if(preg_match("#^http(s)?://[a-z0-9-_.]+\.[a-z]{2,4}#i",$string)){   
     $parsed_url = parse_url($string);   
     $string = $parsed_url['host'].' '.$parsed_url['path'];   
     //if want to add scheme eg. http, https than uncomment next line   
     //$string = $parsed_url['scheme'].' '.$string;  
    }  
    //replace/and . with white space  
    $string = preg_replace("/[\/\.]/", " ", $string);  
    $string = preg_replace("/[^a-z0-9_\s-]/", "", $string);  
    //remove multiple dashes or whitespaces  
    $string = preg_replace("/[\s-]+/", " ", $string);  
    //convert whitespaces and underscore to $replace  
    $string = preg_replace("/[\s_]/", $replace, $string);  
    //limit the slug size  
    $string = substr($string, 0, 100);  
    //slug is generated  
    return ($ext) ? $string.$ext : $string; 

}

建議我的任何變化或新的代碼

回答

1

您可以使用functions prefixed with mb提供支持多字節,例如使用mb_ereg_replace而不是的preg_replace。其他包括mb_substrmb_strtolower

但是,您必須指定編碼,因爲默認編碼是UTF-8而不是unicode。這是使用mb_regex_encoding完成的。