Tuesday, March 16, 2010

PHP TIPS

Parsing Url:Get a query-string variable using php



// Function to parse a url and extract its arguments.
function process_url( $url )
{
$processed_url = parse_url( $url );

$query_string = $processed_url[ 'query' ];
# split into arguments and values
$query_string = explode( '&', $query_string );
$args = array( ); // return array
//foreach( $query_string as $chunk )
//{
list($x,$chunk)=each($query_string);
$chunk = explode( '=', $chunk );
echo count( $chunk )."
";
// it's only really worth keeping if the parameter
// has an argument.
//if ( count( $chunk ) == 2 )
//{
list( $key, $val ) = $chunk;
$args[ $key ] = urldecode( $val );

echo $key."
".$args[$key];
//}
//}
return $args;
}
$url =
'http://www.google.co.nz/search?topics=99&delete=true';
$result = process_url( $url );
print_r( $result );
?>

No comments:

Post a Comment