douquanzhan0315 2017-06-11 12:20
浏览 26

使用preg_replace将永久链接转换为SEO友好永久链接

how to convert permalink

'watch.php?a=RANDOMTEXT1&e=RANDOMTEXT2'

to

'watch/a/RANDOMTEXT1/e/RANDOMTEXT2.html'

with php preg_replace if RANDOMTEXT1 and RANDOMTEXT2 is random text?

  • 写回答

1条回答 默认 最新

  • dou9022 2017-06-11 18:26
    关注

    You could use e.g. this regex:

    (watch)\.php\?(a)=([^&]+)&(e)=([^&]+)
    

    And replace it with:

    $1/$2/$3/$4/$5.html
    

    But it really depends on your requirements. Is this the only example where you need to create a link like this? You could also simplify the regex and only use watch\.php\?a=([^&]+)&e=([^&]+) and replace it with watch/a/$1/e/$2.html. If you want the regex to also allow & character, but only if they are not followed by an equal sign =, you would need to use negative lookaheads.

    Here is the live example: https://regex101.com/r/jdRaok/1

    评论

报告相同问题?

悬赏问题

  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection