php preg_match get everything after match in string

lickmycode picture lickmycode · May 28, 2014 · Viewed 7.6k times · Source

Looking for how to get the complete string in a URI, after the away?to=

My code:

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>";

The original link is:

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

.. but my code is cutting the string after the away?to= to only

http://www.zdf.de/ZDFmediathek

You know the fix for this preg_match function to allow really every character following the away?to= ??

UPDATE:

Found out, that $_SERVER['REQUEST_URI'] or $_SERVER['QUERY_STRING'] is already cutting the original URL. Do you know why and how to prevent that?

Answer

Rakesh Sharma picture Rakesh Sharma · May 28, 2014

try use (.*) to get all after to=

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