来自网址的Preg_match_all

时间:2015-05-25 16:19:21

标签: php

如何获取所有$ _GET name =>来自url的值使用preg_match_all

示例url1:http://www.example.com?go1=test1

示例url2:http://www.example.com?go2=test2

示例url3:http://www.example.com?go1=test1&go3=test3

如果可能,返回需要通过数组

$array = array();
$array[name1] = value1;
$array[name2] = value2;

...

3 个答案:

答案 0 :(得分:1)

如下:

$url = 'http://www.example.com?go1=test1&go3=test3';
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $array);

这会将查询字符串参数放在$array

答案 1 :(得分:0)

这可以帮助您从网址获取变量,也可以使用parse_url获取单个网址组件,例如

使用parse_url:

$url='http://www.example.com?go1=test1&go3=test3';
var_dump(parse_url($url)['query']);

参见演示http://codepad.viper-7.com/AVFDdy

使用preg_match_all:

<?php
$re = "/\\?(.*)/"; 
$str = "http://www.example.com?go1=test1"; 

preg_match_all($re, $str, $matches);
echo '<pre>';
print_r($matches);
echo '</pre>';

请参阅正则表达式https://regex101.com/r/eB0rQ2/1

答案 2 :(得分:0)

尝试查看$ _SERVER [&#39; QUERY_STRING&#39;]选项,希望它会有所帮助。