【问题标题】:PHP preg replace for clean urlPHP preg 替换干净的 url
【发布时间】:2017-11-13 11:15:41
【问题描述】:

什么是这种替换的最佳解决方案? 快速和优化

我的链接:

https://example.com/event/123/Kinda Don't Care2 Feat. Jaba/Country Music (1990)

我要干净到:

https://example.com/event/123/Kinda-Dont-Care2-Feat-Jaba/Country-Music-1990

preg_replace('???????', '-', $strUrl);

编辑:

$str1 = "Kinda Don't Care2 Feat. Jaba";

$str2 = "Country Music (1990)";

替换为:

$str1 = "Kinda-Dont-Care2-Feat-Jaba";

$str2 = "Country-Music-1990";

谢谢。

【问题讨论】:

标签: php regex preg-replace


【解决方案1】:

试试这个

$string = "example.com/event/123/Kinda Don't Care2 Feat. Jaba/Country Music (1990)";

    // remove all non alphanumeric characters except spaces
    $clean =  preg_replace('/[^a-zA-Z0-9\s]/', '', strtolower($html)); 

    // replace one or multiple spaces into single dash (-)
    $clean =  preg_replace('!\s+!', '-', $clean); 

    echo $clean;

参考-Automatic clean and SEO friendly URL (slugs)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多