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 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改