I have url like this :
/leads/set_attributes.html?lead_id=3793&name=TEST
I need remove 'name' and return url.
I try do it like this:
$vars = [];
parse_str(html_entity_decode($sAddQuery), $vars);
unset($vars['name']);
$sAddQuery = http_build_query($vars);
But i always get
%3Flead_id=3793
as result. I tried use html_entity_decode for array elements but it did not help. How can i solve this? Thanks
Modify your code to :
<?php
$url = parse_url('/leads/set_attributes.html?lead_id=3793&name=TEST');
$str = $url['query'];
parse_str($str, $params); //parse URL
unset($params['name']); //unset name parameter
$string = http_build_query($params); //again built the URL string
var_dump($string);
?>
Hope it works!
Related
I have a variable in my PHP script called $url
Here's an example of how I use it:
<?php
$url = '/test/?utm_source=test&utm_campaign=test2&utm_medium=test3";
I'd like to have a PHP snippet that grabs the valus of utm_source, utm_campaign & utm_medium. How do I achieve this?
Try This
<?php
$url = "/test/?utm_source=test&utm_campaign=test2&utm_medium=test3";
$url_parsed=parse_url($url);
$query_params=[];
$query_parsed=parse_str($url_parsed['query'],$query_params);
echo $query_params['utm_source'].PHP_EOL.
$query_params['utm_campaign'].PHP_EOL.
$query_params['utm_medium'].PHP_EOL;
?>
Another way with explode function:
<?php
$url = "/test/?utm_source=test&utm_campaign=test2&utm_medium=test3";
$params = explode("=", $url);
$utmSource = explode("&", $params[1]);
$utmCampaign = explode("&", $params[2]);
$utmMedium = $params[3];
I have a feeling that the function parse_str() is exactly what you are looking for. See https://secure.php.net/manual/en/function.parse-str.php
I have this url:
/?goto=%2Fr%2Faccount%2Findex%2Ecfm%3Fsite_id%3D87211
How can I GET site_id from this url?
Check this way,
<?php
$abc ="/?goto=%2Fr%2Faccount%2Findex%2Ecfm%3Fsite_id%3D87211";
$test = parse_url($abc);
$test = urldecode($test["query"]);
$url = parse_url($test);
parse_str($url['query'], $param);
print_r($param["site_id"]);
?>
Check your output here :https://eval.in/621360
Try parse_str and parse_url on $_GET['goto'].
parse_str(parse_url($_GET['goto'], PHP_URL_QUERY), $params);
$site_id = $params['site_id'];
If you know your url's will always be in this structure, you can parse it out with RegEx like so:
$url = '/?goto=%2Fr%2Faccount%2Findex%2Ecfm%3Fsite_id%3D87211';
preg_match('/site_id\%3D(\d+)/', $url, $matches);
$siteId = $matches[1];
It's searching for anything that matches site_id%3D and the numeric value that follows that.
Try using rawurldecode in URL function
by using $_GET['goto'] you will get following url :
/r/account/index.cfm?site_id=87211
Try this
$uri = $_GET['goto'];
$site = explode("=", $uri);
echo $site[1];
Example:
$url = http://example.com/?arg=val&arg2=test&arv3=testing&arv2=val2
remove_url_arg($url, "arg2")
echo($url); // http://example.com/?arg=val&arv3=testing
The above remove_url_arg() function removes all occurrence of arg2 argument from the URL
unset($_GET['arg2']);
$query_string = http_build_query($_GET);
if it's not on request but to parse whole url
$parsed = parse_url($url);
$qs_arr = parse_str($parsed['query'],1);
unset($qs_arr['arg2']);
$parsed['query'] = http_build_query($qs_arr);
and then assemble the url back.
or one-liner regexp
Here is the url:
http://localhost/test.php?id=http://google.com/?var=234&key=234
And I can't get the full $_GET['id'] or $_REQUEST['d'].
<?php
print_r($_REQUEST['id']);
//And this is the output http://google.com/?var=234
//the **&key=234** ain't show
?>
$get_url = "http://google.com/?var=234&key=234";
$my_url = "http://localhost/test.php?id=" . urlencode($get_url);
$my_url outputs:
http://localhost/test.php?id=http%3A%2F%2Fgoogle.com%2F%3Fvar%3D234%26key%3D234
So now you can get this value using $_GET['id'] or $_REQUEST['id'] (decoded).
echo urldecode($_GET["id"]);
Output
http://google.com/?var=234&key=234
To get every GET parameter:
foreach ($_GET as $key=>$value) {
echo "$key = " . urldecode($value) . "<br />\n";
}
$key is GET key and $value is GET value for $key.
Or you can use alternative solution to get array of GET params
$get_parameters = array();
if (isset($_SERVER['QUERY_STRING'])) {
$pairs = explode('&', $_SERVER['QUERY_STRING']);
foreach($pairs as $pair) {
$part = explode('=', $pair);
$get_parameters[$part[0]] = sizeof($part)>1 ? urldecode($part[1]) : "";
}
}
$get_parameters is same as url decoded $_GET.
While creating url encode them with urlencode
$val=urlencode('http://google.com/?var=234&key=234')
Click here
and while fetching decode it wiht urldecode
You may have to use urlencode on the string 'http://google.com/?var=234&key=234'
I had a similar problem and ended up using parse_url and parse_str, which as long as the URL in the parameter is correctly url encoded (which it definitely should) allows you to access both all the parameters of the actual URL, as well as the parameters of the encoded URL in the query parameter, like so:
$get_url = "http://google.com/?var=234&key=234";
$my_url = "http://localhost/test.php?id=" . urlencode($get_url);
function so_5645412_url_params($url) {
$url_comps = parse_url($url);
$query = $url_comps['query'];
$args = array();
parse_str($query, $args);
return $args;
}
$my_url_args = so_5645412_url_params($my_url); // Array ( [id] => http://google.com/?var=234&key=234 )
$get_url_args = so_5645412_url_params($my_url_args['id']); // Array ( [var] => 234, [key] => 234 )
you use bad character like ? and & and etc ...
edit it to new code
see this links
http://antoine.goutenoir.com/blog/2010/10/11/php-slugify-a-string/
http://sourcecookbook.com/en/recipes/8/function-to-slugify-strings-in-php
also you can use urlencode
$val=urlencode('http://google.com/?var=234&key=234')
The correct php way is to use parse_url()
http://php.net/manual/en/function.parse-url.php
(from php manual)
This function parses a URL and returns an associative array containing any of the various components of the URL that are present.
This function is not meant to validate the given URL, it only breaks it up into the above listed parts. Partial URLs are also accepted, parse_url() tries its best to parse them correctly.
if (isset($_SERVER['HTTPS'])){
echo "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]$_SERVER[QUERY_STRING]";
}else{
echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]$_SERVER[QUERY_STRING]";
}
I have a question, How I can add another get variable in my current url
book.php?action=addressbook
i want to add
book.php?action=addressbook&page=2
how to generate hyperlink for this, i have tried it using $_SERVER['PHP_SELF'] but query string are not included in url its showing something like that
book.php?page=2
I want to append my other variables to query string
Please help
You can also use http_build_query(); to append more params to your URL
$params = $_GET;
$params["item"] = 45;
$new_query_string = http_build_query($params);
for instance:
$data = array('page'=> 34,
'item' => 45);
echo http_build_query($data); //page=34&item=45
or include amp
echo http_build_query($data, '', '&'); //page=34&&item=45
$get = $_GET;
$get['page'] = 2;
echo 'Page 2';