Return to Snippet

Revision: 5165
at February 16, 2008 05:51 by dbug13


Updated Code
<?php
$x = "/blog/index/1?foo=2&bar=4";
//$x = "/blog/index/1";
$x = preg_replace('#^/(.*)#', "$1", $x );

$url_data = explode("?", $x);
$url_part = $url_data[0];
$query_part = "";
$query_parms = array();

if(count($url_data) > 1){
	$query_part = $url_data[1];
}

// parse url part 
$temp_url_parts = explode("/", $url_part);
$controller = "";
$action = "";
$id = "";
if (count($temp_url_parts) ){
	$controller = $temp_url_parts[0];
	
	if (count($temp_url_parts) > 1){
		$action = $temp_url_parts[1];
	}
	
	if (count($temp_url_parts) > 2){
		$id = $temp_url_parts[2];
	}
}

// parse query params
if (count($url_data) > 1){
	$query_part = $url_data[1];
	
	$temp_query_parts = explode("&", $query_part);
	foreach($temp_query_parts as $q){
		$temp = explode("=", $q);
		$key = $temp[0];
		$value = $temp[1];
		$query_parms[$key] = $value;
	}
}

$parms = array(
	"controller" => $controller,
	"action" => $action,
	"id" => $id,
	"get" => $query_parms,
	"post" => $_POST
);

print_r($parms);
?>

Revision: 5164
at February 15, 2008 17:02 by dbug13


Updated Code
<?php
$x = "/blog/index/1?foo=2&bar=4";
//$x = "/blog/index/1";
$x = preg_replace('#^/(.*)#', "$1", $x );

$data = explode("/", $x);
$query_parms = array();

$id_data = explode("?", $data[count($data)-1]);
$id = $id_data[0];
if (count($id_data) > 1){
	$query_data = $id_data[1];
	$query_data = explode("&", $query_data);
	
	foreach($query_data as $q){
		$temp = explode("=", $q);
		$key = $temp[0];
		$value = $temp[1];
		$query_parms[$key] = $value;
	}
}

$params = array(
	"controller" => $data[0],
	"action" => $data[1],
	"id" => $id,
	"get" => $query_parms,
	"post" => $_POST
);

print_r($params);

?>

Revision: 5163
at February 15, 2008 17:00 by dbug13


Initial Code
<?php
$x = "/blog/index/1?foo=2&bar=4";
//$x = "/blog/index/1";
$x = preg_replace('#^/(.*)#', "$1", $x );

$data = explode("/", $x);
$query_parms = array();

$id_data = explode("?", $data[count($data)-1]);
$id = $id_data[0];
if (count($id_data) > 1){
	$query_data = $id_data[1];
	$query_data = explode("&", $query_data);
	
	foreach($query_data as $q){
		$temp = explode("=", $q);
		$key = $temp[0];
		$value = $temp[1];
		$query_parms[$key] = $value;
	}
}

$params = array(
	"controller" => $data[0],
	"action" => $data[1],
	"id" => $id,
	"get" => $query_parms,
	"post" => $_POST
);

print_r($params);

?>

Initial URL

                                

Initial Description

                                

Initial Title
PHP URL parsing

Initial Tags
url, php, textmate

Initial Language
PHP