Wednesday, 8 October 2014

convert string to url get in PHP



<?php

function stringtoURL($string,$set=TRUE){
$strPos = strpos($string,'?');
$str = substr($string,$strPos+1);
$groups = explode('&',$str);
$nSet = array();
foreach($groups as $st){
list($name,$var) = explode('=',$st);
if($set){
$_GET[$name] = $var;
}else{
$nSet[$name] = $var;
}
}
if(!$set){
return $nSet;
}
}

// Version 1
// Convert string to $_GET variables
$s = 'http://tzfiles.com/?name=bob&str=hello&q=awesome';
stringtoURL($s);
echo $_GET['name'].'<br />';
echo $_GET['str'].'<br />';
echo $_GET['q'].'<br />';

// Version 2
// Convert string to an array
$s = 'http://tzfiles.com/?name=bob&str=hello&q=awesome';
print_r(stringtoURL($s,false));
?>

0 comments:

Post a Comment