Mysql make slug function
程序员文章站
2022-05-18 14:11:33
...
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/
推荐阅读
-
IIS下PHP连接数据库提示mysql undefined function mysql_connect()
-
Mysql5.7创建存储过程中调用自定义函数报错Not allowed to return a result set from a function
-
MySQL 5.7 create VIEW or FUNCTION or PROCEDURE
-
MySQL与Oracle差异比较之五存储过程&Function
-
sql2k中新增加的Function的sqlbook 的帮助_MySQL
-
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES),该如何处理
-
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES),该如何处理
-
make me cry make me smile php入门学习知识点一 PHP与MYSql连接与查询
-
php连接mysql出错:Fatal error: Call to undefined function mysql_connect(),该如何处理
-
[php]php连mysql出错:Call to undefined function mysql_connect()