doupaoshu8334 2015-12-08 11:49
浏览 114

Mysql匹配保存的正则表达式的url

I have a table in which I am saving the regex expression that later I used to detect the urls.

Now I want if I enter url in search field then in the database query will fetch all the records(regex expression) that catch the url:

for example in the db I have saved this expression

.*\/[a-z0-9]{0,}\.gif\?abc=[0-9]&rnd=[0-9]{1,}.*

Now If I search this url

cnzz.mmstat.com/9.gif?abc=1&rnd=162146281

then it should automatically fetch the record where regex detect this url.

I am using MySql and Core PHP

  • 写回答

1条回答 默认 最新

  • dongshou6041 2015-12-08 11:54
    关注

    You don't have any capture groups in your regex. Use this:

    .*\/[a-z0-9]{0,}\.gif\?abc=[0-9]&rnd=([0-9]{1,}.*)
    

    So this gives the last set of numbers as a capture group. You can test it at Regex101.

    Now using preg_match() you can get the last id of the URL and that can be used to make your calculations.

    Sample Code

    <?php
    preg_match('.*\/[a-z0-9]{0,}\.gif\?abc=[0-9]&rnd=([0-9]{1,}.*)',
        "cnzz.mmstat.com/9.gif?abc=1&rnd=162146281", $matches);
    $id = $matches[0];
    
    echo "The id is: {$matches[0]}
    ";
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?