douqingnao9246 2017-05-15 12:50
浏览 23
已采纳

如何创建正确的preg_replace模式?

I have these urls in my magento store: charogh/product/1298/productname the productname will be change for each product,the correct structure is: charogh/product/[0-9]+/[a-zA-z0-9]+

I should change and replace 'product/[0-9]+/' with '' and make the url to 'charogh/' when user enter this url 'charogh/product/[0-9]+/' and do not replace it when user enter this url 'charogh/product/[0-9]+/productname' how can i do it with preg_replace or preg_match?

I use this pattern but it replace in both structures $header=preg_replace('//charogh/product/[0-9]+/',"charogh",$header);

thanks for your help!

  • 写回答

1条回答 默认 最新

  • dongshi9407 2017-05-15 12:56
    关注

    I suggest you anchor the match at the string end with the $ anchor:

    '/\/charogh\/product\/[0-9]+\/?$/'
    

    This way you will prevent matching strings that contain more data after 1+ digits.

    Also, the \/? at the end allows matching 1 or 0 / symbols (i.e. the / is optional).

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

报告相同问题?

悬赏问题

  • ¥15 请问各位,如何在Jetson nano主控板的Ubuntu系统中安装PyQt5
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥150 求 《小魔指》街机游戏机整合模拟软件