dongzhou1901 2013-06-16 00:41
浏览 46
已采纳

从引用URL的一部分创建php变量

I am trying to work out a script to log IPs from poll votes on a message board, that will only be fired off if a vote is cast in one of our polls. Edit: I'm doing this via a web beacon, because I don't have access to the poll's programming. /edit

When the script fires off, it needs to know which poll is being voted in, since there are often multiple polls that are open at the same time, and log the IP of voters in a flat file that is dedicated to that poll.

First, I grab the referring URL, which is formatted like this:

http://subdomain.sample.com/t12345,action=vote

If 'vote' is found in the referring URL, the next thing I want to do is grab that t# and turn it into a variable, so I can log info in a file named t12345.txt or 12345.txt, either-or doesn't really matter, as long as it matches the topic number of the poll.

The numbers after the /t are the only thing that should change in this URL. There are currently 5 digits here, and I don't expect this to change any time soon.

My question is: How do I grab this t# from the URL and create a variable from it?

Thank you in advance!

  • 写回答

4条回答 默认 最新

  • dongyao1895 2013-06-16 00:49
    关注

    Check out preg_match

    preg_match('|/t[0-9]{5}|', $url, $matches);
    if (count($matches)) {
        $t_number = $matches[0]; // "/t12345"
        $number = substr($t_number, 2, strlen($t_number)); // 12345
    }
    

    Assumptions:

    1) The referring url will never have the pattern t#####. (t12345.com/vote)

    2) You will always have five digits. (if this changes, you can do {5,6} to match 5-6 instances

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM