首頁 > 資料庫 > mysql教程 > Mysql make slug function

Mysql make slug function

WBOY
發布: 2016-06-01 13:08:02
原創
1042 人瀏覽過

New update on slugify

DROP FUNCTION IF EXISTS `slugify`;DELIMITER ;;CREATE DEFINER=CURRENT_USERFUNCTION `slugify`(dirty_string varchar(200))RETURNS varchar(200) CHARSET latin1DETERMINISTICBEGIN	DECLARE x, y , z, i Int;	Declare temp_string, allowed_chars, new_string VarChar(200);	Declare is_allowed Bool;	Declare c, check_char VarChar(1);	set i = 0;	set allowed_chars = "abcdefghijklmnopqrstuvwxyz0123456789-";	set temp_string = LOWER(dirty_string);	Select temp_string Regexp('&') Into x;	If x = 1 Then		Set temp_string = replace(temp_string, '&', ' and ');	End If;	Select temp_string Regexp('[^a-z0-9]+') into x;	If x = 1 then		set z = 1;		While z  1 Do			If Strcmp(SubString(temp_string, -1, 1), '-') = 0 Then				Set temp_string = Substring(temp_string,1, y-1);				Set y = y - 1;			Else				Leave Dash_check;			End If;			Set z = z - 1;		End While;	End If;	Repeat		Select temp_string Regexp("--") into x;		If x = 1 Then			Set temp_string = Replace(temp_string, "--", "-");		End If;	Until x  1 End Repeat;	If LOCATE('-', temp_string) = 1 Then		Set temp_string = SUBSTRING(temp_string, 2);	End If;SELECT COUNT(*) INTO i FROM fanpage WHERE slug LIKE CONCAT(temp_string,'%');If i > 0 Then	Set temp_string = CONCAT(temp_string,'-',i+1);End If;Return temp_string;END;;DELIMITER ;
登入後複製

P.S: change “fanpage” to your own table – this is to prevent duplicate on slug

credit goes to http://nastyhabit.wordpress.com/2008/09/25/mysql-slug-maker-function-aka-the-slugifier/

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板