PHP:如何获得$ _GET变量的中间值?

时间:2014-02-06 04:24:47

标签: php jquery string variables get

我有一个$_GET变量,它是md5('currentdate') + ID +另一个加密随机字符串的组合。希望有这个,我可以以某种方式保护我的URL。

示例网址:

create_pdf.php?goto=qredit&qrid=1e02b73e2b1cb88685499f38508cf6e84317e62166fc8586dfa4d1bc0e1742c08b
                                                                ^^

我需要获取$_GET['qrid']变量的中间位置,在本例中为43。 我尝试使用substr(),但我没有得到我想要的结果。

更新 以下是我生成$_GET变量的方法。

<a href="create_pdf.php?goto=qredit&qrid=<?php echo md5($today).$_GET['qrid'].md5($_GET['qrid']); ?>" target="_blank" id="upload_button">-random whatnot-</a>

2 个答案:

答案 0 :(得分:0)

假设第一个和第二个字符串是固定的32个字符,请尝试从字符串中检索ID值:

// Get all characters up to the last 32 chracters, starting at 32nd character
$id = substr($_GET['qrid'], 32, strlen($_GET['qrid'])-64);

答案 1 :(得分:0)

可以使用正则表达式来实现更清洁的解决方案。假设它是MD5,前端和结束字符串总是32。中间捕获组将从0开始获取您的ID。

preg_match('/\d{32}(\w+)\d{32}/', $_GET['qrid'], $matches);
$id = $matches[1];