duanpang5583 2010-04-22 09:22
浏览 153
已采纳

从URL中删除特殊字符

I have a product database and I am displaying trying to display them as clean URLs, below is example product names:

PAUL MITCHELL FOAMING POMADE (150ml)
American Crew Classic Gents Pomade 85g
Tigi Catwalk Texturizing Pomade 50ml

What I need to do is display like below in the URL structure:

www.example.com/products/paul-mitchell-foaming-gel(150ml)

The problem I have is I want to do the following:

1.  Remove anything inside parentheses (and the parentheses)
2.  Remove any numbers next to g or ml e.g. 400ml, 10g etc...

I have been banging my head trying different string replaces but cant get it right, I would really appreciate some help.

Cheers

  • 写回答

4条回答 默认 最新

  • drema2014 2010-04-22 09:29
    关注
    function makeFriendly($string)
    {
        $string = strtolower(trim($string));
        $string = str_replace("'", '', $string);
        $string = preg_replace('#[^a-z\-]+#', '_', $string);
        $string = preg_replace('#_{2,}#', '_', $string);
        $string = preg_replace('#_-_#', '-', $string);
        return preg_replace('#(^_+|_+$)#D', '', $string);
    }
    

    this function helps you for cleaning url. (also cleans numbers)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本