dptj13337 2017-06-12 04:55
浏览 20
已采纳

用于清理网址的PHP preg_replace

what is best solution for this replacing? fast and optimized

my link:

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

I want clean to:

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

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

Edit:

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

$str2 = "Country Music (1990)";

replace to :

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

$str2 = "Country-Music-1990";

thanks.

  • 写回答

1条回答 默认 最新

  • dongyin6576 2017-06-12 05:00
    关注

    try this

    $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;
    

    ref- Automatic clean and SEO friendly URL (slugs)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据