php preg_match在字符串匹配后获取所有内容

时间:2014-05-28 10:03:35

标签: php preg-match request-uri

在离开?to =

之后,查看如何在URI中获取完整的字符串

我的代码:

if (isset($_SERVER[REQUEST_URI])) {
    $goto = $_SERVER[REQUEST_URI];
}

if (preg_match("/to=(.+)/", $goto, $goto_url)) {

$link = "<a href='{$goto_url[1]}' target='_blank'>{$goto_url[1]}</a>";

原始链接是:

https://domain.com/away?to=http://www.zdf.de/ZDFmediathek#/beitrag/video/2162504/Verschw%C3%B6rung-gegen-die-Freiheit-%281%29

..但我的代码是在离开之后切割字符串?to =

http://www.zdf.de/ZDFmediathek

你知道这个preg_match函数的修复程序是否允许之后的每个字符都离开?to = ??

更新

发现,$_SERVER['REQUEST_URI']$_SERVER['QUERY_STRING']已经删除了原始网址。你知道为什么以及如何预防吗?

2 个答案:

答案 0 :(得分:2)

尝试使用(.*)获取to=

之后的全部内容
$str = 'away?to=dfkhgkjdshfgkhldsflkgh';
preg_match("/to=(.*)/", $str, $goto_url);
echo $goto_url[1]; //dfkhgkjdshfgkhldsflkgh 

答案 1 :(得分:0)

不是从请求URI中使用正则表达式提取URL,而是从$ _GET数组中获取它:

$link = "<a href='{$_GET['to']}' target='_blank'>{$_GET['to']}</a>";
相关问题