I'm not sure exactly what you are asking, so here is the answer in both directions:
If you have the full URL that you are trying to parse, use parse_url:
$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
The above example outputs:
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
If you have only part of the URL request, you can use parse_str:
parse_str($str, $output);
echo $output['first']; // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
If you have the url you are trying to use, use http_build_query:
$data = array('foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertext processor');
echo http_build_query($data);
, , filter_input / PHP:
http://us2.php.net/manual/en/ref.filter.php
http://us2.php.net/manual/en/function.filter-input-array.php
http://us2.php.net/manual/en/filter.filters.validate.php
http://us2.php.net/manual/en/filter.filters.sanitize.php
filter_validate_array :
$args = array(
'product_id' => FILTER_SANITIZE_ENCODED,
'component' => array('filter' => FILTER_VALIDATE_INT,
'flags' => FILTER_REQUIRE_ARRAY,
'options' => array('min_range' => 1, 'max_range' => 10)
),
'versions' => FILTER_SANITIZE_ENCODED,
'doesnotexist' => FILTER_VALIDATE_INT,
'testscalar' => array(
'filter' => FILTER_VALIDATE_INT,
'flags' => FILTER_REQUIRE_SCALAR,
),
'testarray' => array(
'filter' => FILTER_VALIDATE_INT,
'flags' => FILTER_REQUIRE_ARRAY,
)
);
$myinputs = filter_input_array(INPUT_POST, $args);
var_dump($myinputs);
echo "\n";
:
array(6) {
["product_id"]=>
array(1) {
[0] => string(17) "libgd%3Cscript%3E"
}
["component"]=>
array(1) {
[0] => int(10)
}
["versions"]=>
array(1) {
[0] => string(6) "2.0.33"
}
["doesnotexist"]=>
NULL
["testscalar"]=>
bool(false)
["testarray"]=>
array(1) {
[0] => int(2)
}
}