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条)

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable