繁体   English   中英

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

[英]php preg_match get everything after match in string

寻找离开后如何获取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

..但我的代码是在走远之后将字符串切掉?

http://www.zdf.de/ZDFmediathek

您知道此preg_match函数的修复程序,它允许真正的每个字符跟随走?

更新:

发现, $_SERVER['REQUEST_URI']$_SERVER['QUERY_STRING']已在切割原始URL。 您知道为什么以及如何预防吗?

尝试使用(.*)来获取所有后缀to=

$str = 'away?to=dfkhgkjdshfgkhldsflkgh';
preg_match("/to=(.*)/", $str, $goto_url);
echo $goto_url[1]; //dfkhgkjdshfgkhldsflkgh 

不用从请求URI中使用正则表达式提取URL,您只需从$ _GET数组中获取即可:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM