php – mySQL存储功能来创建一个slug

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – mySQL存储功能来创建一个slug脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有一个 @L_403_0@存储的功能,从一个url(或任何值真的)创建一个slug.

所以我的查询可以是:

SELECT *,SLUG(url) From clients
这是上述功能的更新版本.它更快,因为它避免循环所有允许的字符,只是通过比较ASCII代码进行检查.
DROP FUNCTION IF EXISTS `slugify`;
DELIMITER ;;
CREATE defineR=`root`@`localhost`
FUNCTION `slugify`(dirty_string vArchar(200))
RETURNS VARchar(200) CHARSET latin1
DETERMINISTIC
BEgin
    DECLARE x,y,z Int;
    Declare temp_string,new_string VarChar(200);
    Declare is_Allowed Bool;
    Declare c,check_char VarChar(1);

    set temp_string = LOWER(dirty_string);

    Set temp_string = replace(temp_string,'&',' and ');

    Select temp_string Regexp('[^a-z0-9\-]+') into x;
    If x = 1 then
        set z = 1;
        While z <= Char_length(temp_string) Do
            Set c = Substring(temp_string,z,1);
            Set is_allowed = False;
            If !((ascii(c) = 45) or (ascii(c) >= 48 and ascii(c) <= 57) or (ascii(c) >= 97 and ascii(c) <= 122)) Then
                Set temp_string = Replace(temp_string,c,'-');
            End If;
            set z = z + 1;
        End While;
    End If;

    Select temp_string Regexp("^-|-$|'") into x;
    If x = 1 Then
        Set temp_string = Replace(temp_string,"'",'');
        Set z = Char_length(temp_string);
        Set y = Char_length(temp_string);
        Dash_check: 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;

    Return temp_string;
END;;
DELIMITER ;

脚本宝典总结

以上是脚本宝典为你收集整理的php – mySQL存储功能来创建一个slug全部内容,希望文章能够帮你解决php – mySQL存储功能来创建一个slug所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。