Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract parameter from a string

Tags:

string

php

I have to extract a string like this:

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

Now my task is to extract report, action and rname value in PHP.

I have tried by using explode(), but I am not able to extract module.

How can I do it?

like image 980
Deepesh Avatar asked Dec 05 '22 05:12

Deepesh


1 Answers

You could use parse_str() in this case:

$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);

Should output:

Array
(
    [module] => Reports
    [action] => abc
    [rname] => Instantpayment
)
like image 84
Kevin Avatar answered Dec 15 '22 18:12

Kevin