从字符串中提取参数

时间:2014-10-21 06:11:40

标签: php string

我必须提取一个这样的字符串:

index.php?module=Reports&action=abc&rname=Instantpayment

现在我的任务是在PHP中提取reportactionrname值。

我尝试使用explode(),但我无法提取module

我该怎么做?

5 个答案:

答案 0 :(得分:4)

使用$_GET从网址

获取查询字符串
echo $_GET['module']; //Reports
echo $_GET['action']; // abc
echo $_GET['rname'];  // Instantpayment

从字符串中获取explode()

$str ='index.php?module=Reports&action=abc&rname=Instantpayment';
$e = explode('?', $str);
$e1 = explode('&', $e[1]);
foreach($e1 as $v) {
    $ex = explode('=', $v);
    $newarr[$ex[0]] = $ex[1];
}
print_r($newarr); // Use this array of values you want.
//Array ( [module] => Reports [action] => abc [rname] => Instantpayment )
echo  $newarr['module'];
echo  $newarr['action'];
echo  $newarr['rname'];

答案 1 :(得分:4)

在这种情况下,您可以使用parse_str()

$string = 'index.php?module=Reports&action=abc&rname=Instantpayment';
$string = substr($string, strpos($string, '?')+1); // get the string from after the question mark until end of string
parse_str($string, $data); // use this function, stress free

echo '<pre>';
print_r($data);

应输出:

Array
(
    [module] => Reports
    [action] => abc
    [rname] => Instantpayment
)

答案 2 :(得分:4)

$yourUrl="module=Reports&action=abc&rname=Instantpayment"
$exploded_array = array();
parse_str($yourUrl, $exploded_array);
$exploded_array['module']; 
$exploded_array['action'];
$exploded_array['rname'];

答案 3 :(得分:2)

您必须访问全局GET变量:

$_GET['module']
$_GET['action']
$_GET['rname']

答案 4 :(得分:2)

试试这个:

<?php
    $temp = "index.php?module=Reports&action=abc&rname=Instantpayment";

    $t1 = explode("=",$temp);

    for ($i = 1; $i < sizeof($t1); $i++)
    {
        $temp = explode("&", $t1[$i]);
        echo $temp[0] . "\n";
    }
?>