php通过get方法从url中获取url


php get url from url by get method

我的php页面有两个参数$link和$text我想要$link参数和里面的所有参数示例

test.php?链接=www.google.com?test=test&test2=test2&text=测试文本

我想获取链接='www.google.com?test=test&test2=test2'并获取text=testtext

我使用这个php脚本

<?php
      $text = $_GET['text']; 
      $link = $_GET['link'];
      echo  $text;
      echo  $link;
?>
output
testtext
www.google.com?test=test

在GET上使用参数之前,应该对参数进行编码。

echo '<a href="test.php?link=' . urlencode('www.google.com?test=test&test2=test2') . '&text=' . urlencode('testtext') . '">test</a>';

这样一来,googlevars和你的之间就没有冲突了。

有关详细信息,请参阅urlencode()手册。

$link_mod = str_replace("?", "&", $_GET['link']);
$array = explode("&", $link_mod);
unset($array[0]); #get rid of www.google.com segment
$segments = array();
foreach ($array as $line) {
   $line_array = explode('=', $line);
   $key = $line_array[0];
   $value = $line_array[1];
   $segments[$key] = $value;
}
print_r($segments);

如果要将URL作为参数传递,则必须对其进行转义。否则,参数将在脚本中显示为$_GET参数。

您必须使用urlencode():生成链接

$link = "test.php?link=".urlencode("www.google.com?test=test&test2=test2")."&text=" . urlencode("testtext");

使用字符串时也要使用引号:

$text = $_GET['text']; 
$link = $_GET['link'];