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/