I want to select specific words of an url. Let say I have an url localhost/index.php?exam=one&people=two
The question, how to get "one" and "two" with php? I was read preg_match function, but I still confuse with regulation expression pattern. thanks for advance
$query - parse_url('localhost/index.php?exam=one&people=two', PHP_URL_QUERY);
parse_str($query, $keys);
print_r(array_values($keys)); // <- what you want
But beware of magic quotes in PHP < 5.4, parse_str()
is affected by this setting
$_GET['exam']
retrieves one
$_GET['people']
retrieves two
Those are called GET parameters. You can access them using php's hashed variable $_GET['param']
.
In this case $_GET['exam']
would be equal to 'one'
Luckily PHP parses it for you!
Try:
echo $_GET['exam'];
to see how it works.
To help debug things, you might find:
print_r($_GET);
useful.
Use parse_url
to access the query string of the URL. explode
the query string on ampersands to get an array of name=value
pairs and explode those parts on equal-signs to split them into name and value.
you have this in the url? Then you can use $_GET['exam']
and $_GET['people']
. You can read more about this here
You can use parse_str()
, like this:
$queryString = $_SERVER['QUERY_STRING'];
parse_str($queryString, $parsedQueryString);
print_r($parsedQueryString);