I have an MVC framework and I would like to get the name of the module, controller and function from the query string; something like:
Code: Select all
http://mysite.com/index.php?module=shop&controller=cart&function=add&prod=12345Thanks!
Moderator: General Moderators
Code: Select all
http://mysite.com/index.php?module=shop&controller=cart&function=add&prod=12345Code: Select all
<?php // File: test.php
// Here is the regex in verbose commented format.
$re = '/# Regex to match three specific prop=values in URI query string.
\? # Start of query string is a literal "?"
(?> # Atomic group prohibits backtracking.
&?+module=([^&#\s]*+) # $1: Module value.
| &?+controller=([^&#\s]*+) # $2: Controller value.
| &?+function=([^&#\s]*+) # $3: Function value.
| &?+[^=&#\s]*+=[^&#\s]*+ # Match and discard any other "prop=value".
)*+ # Loop through all "prop=value" in query.
(?:\#|$) # End of query is EOS or start of fragment.
/ix';
// Some example data to test different orders and missing values
$data = array(
// Test parameters occuring in different orders.
"http://mysite.com/index.php?module=shop&controller=cart&function=add&prod=12345", // 1
"http://mysite.com/index.php?controller=cart&function=add&prod=12345&module=shop", // 2
"http://mysite.com/index.php?function=add&prod=12345&module=shop&controller=cart", // 3
"http://mysite.com/index.php?prod=12345&module=shop&controller=cart&function=add", // 4
// Test ending the query with a fragment rather than end of string.
"http://mysite.com/index.php?prod=12345&module=shop&controller=cart&function=add#fragment", // 5
// Test what happens when there are missing expected parameters.
"http://mysite.com/index.php?controller=cart&function=add&prod=12345", // 6
"http://mysite.com/index.php?module=shop&function=add&prod=12345", // 7
"http://mysite.com/index.php?module=shop&controller=cart&prod=12345", // 8
"http://mysite.com/index.php?module=shop", // 9
"http://mysite.com/index.php?&prod=12345", // 10
);
// Ok lets do this...
for ($i = 0; $i < count($data); ++$i) {
if (preg_match($re, $data[$i], $matches)) {
printf("\nMatch number %d:\n", $i + 1);
if (!isset($matches[1])) $matches[1] = '(not in query string)';
if (!isset($matches[2])) $matches[2] = '(not in query string)';
if (!isset($matches[3])) $matches[3] = '(not in query string)';
printf("\tmodule = %s\n", $matches[1]);
printf("\tcontroller = %s\n", $matches[2]);
printf("\tfunction = %s\n", $matches[3]);
}
}
?>Code: Select all
O:\TEST_PHP>php test.php
Match number 1:
module = shop
controller = cart
function = add
Match number 2:
module = shop
controller = cart
function = add
Match number 3:
module = shop
controller = cart
function = add
Match number 4:
module = shop
controller = cart
function = add
Match number 5:
module = shop
controller = cart
function = add
Match number 6:
module =
controller = cart
function = add
Match number 7:
module = shop
controller =
function = add
Match number 8:
module = shop
controller = cart
function = (not in query string)
Match number 9:
module = shop
controller = (not in query string)
function = (not in query string)
Match number 10:
module = (not in query string)
controller = (not in query string)
function = (not in query string)
O:\TEST_PHP>