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
Related
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!
I'm trying to get a very specific part of a URL using PHP so that I can use it as a variable later on.
The URL I have is:
https://forums.mydomain.com/index.php?/clubs/11-Default-Club
The particular part I am trying to extract is the 11 part between the /clubs/ and -Default-Club bits.
I was wondering what the best way to do this was. I've seen examples on here that use a regex-esque parser but I can't wrap my head around it for this particular instance.
Thanks
Edit; this is what I've tried so far using an explode query, but it seems to give me all sorts of elements which are not present in the URL above:
$url = $_SERVER['REQUEST_URI'];
$url = explode('/', $url);
$url = array_filter($url);
$url = array_merge($url, array());
Which returns:
Array ( [0] => index.php?app=core&module=system&controller=widgets&do=getBlock&blockID=plugin_9_bimBlankWidget_dqtr03ssz&pageApp=core&pageModule=clubs&pageController=view&pageArea=header&orientation=horizontal&csrfKey=8e19769b95c733b05439755827a98ac8 )
If you expect that the string with dashes (11-Default-Club) will be always at the end you can try this:
$url = $_SERVER['REQUEST_URI'];
$urlParts = explode('/', $url);
$string = end($urlParts);
$stringParts = explode('-', $string);
$theNumber = $stringParts[0]; // this will be 11
I'd rather be explicit:
<?php
$url = 'https://forums.mydomain.com/index.php?/clubs/11-Default-Club';
$query = parse_url($url, PHP_URL_QUERY);
$pattern = '#^/clubs/(\d+)[a-zA-Z-]+$#';
$digits = preg_match($pattern, $query, $matches)
? $matches[1]
: null;
var_dump($digits);
Output:
string(2) "11"
If this URL structure is fix for all URLs in your site and you only want to get the integer/number/digit part of the URL:
<?php
$url = 'https://forums.mydomain.com/index.php?/clubs/11-Default-Club';
$int = (int) filter_var($url, FILTER_SANITIZE_NUMBER_INT);
echo $int;
If this url structure is fix for all URLs in your site then below is best way to get your value.
<?php
$url = "https://forums.mydomain.com/index.php?/clubs/11-Default-Club";
$url = explode('/', $url);
$url = array_filter($url);
$end = end($url);
$end_parts = explode('-',$end);
echo $end_parts[0];
Output:
11
How can one dynamically find and remove the last child of a website path URI?
Code: $uri = $_SERVER["REQUEST_URI"];
Result: http://192.168.0.16/wordpress/blog/page-2/
Desired result: http://192.168.0.16/wordpress/blog/
Many thanks in advance!
you can use this and you can get your required output:
// implode string into array
$url = "http://192.168.0.16/wordpress/blog/page-2/";
//then remove character from right
$url = rtrim($url, '/');
// then explode
$url = explode('/', $url);
// remove the last element and return an array
json_encode(array_pop($url));
// implode again into string
echo implode('/', $url);
another approach is:
// implode string into array
$url = explode('/', 'http://192.168.0.16/wordpress/blog/page-2/');
//The array_filter() function filters the values of an array using a callback function.
$url = array_filter($url);
// remove the last element and return an array
array_pop($url);
// implode again into string
echo implode('/', $url);
$url = 'http://192.168.0.16/wordpress/blog/page-2/';
// trim any slashes at the end
$trim_url = rtrim($url,'/');
// explode with slash
$url_array = explode('/', $trim_url);
// remove last element
array_pop($url_array);
// implade with slash
echo $new_url = implode('/', $url_array);
Output:
http://192.168.0.16/wordpress/blog
The correct way would be to use parse_url() and dirname(), which will also support query params. You could explode $uri['path'] but its unnecessary in this case.
<?php
// explode the uri in its proper parts
$uri = parse_url('/wordpress/blog/page-2/?id=bla');
// remove last element
$path = dirname($uri['path']);
// incase you got query params, append them
if (!empty($uri['query'])) {
$path .= '?'.$uri['query'];
}
// string(22) "/wordpress/blog?id=bla"
var_dump($path);
See it working: https://3v4l.org/joJrF
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];
Is there any reliable way in PHP to clean a URL of anchor tags?
So input:
http://site.com/some/#anchor
Outputs:
http://site.com/some/
Using strstr()
$url = strstr($url, '#', true);
Using strtok()
Shorter way, using strtok:
$url = strtok($url, "#");
Using explode()
Alternative way to separate the url from the hash:
list ($url, $hash) = explode('#', $url, 2);
If you don't want the $hash at all, you can omit it in list:
list ($url) = explode('#', $url);
With PHP version >= 5.4 you don't even need to use list:
$url = explode('#', $url)[0];
Using preg_replace()
Obligatory regex solution:
$url = preg_replace('/#.*/', '', $url);
Using Purl
Purl is neat URL manipulation library:
$url = \Purl\Url::parse($url)->set('fragment', '')->getUrl();
There is also one other option with parse_url();
$str = 'http://site.com/some/#anchor';
$arr = parse_url($str);
echo $arr['scheme'].'://'.$arr['host'].$arr['path'];
Output:
http://site.com/some/
Alternative way
$url = 'http://site.com/some/#anchor';
echo str_replace('#'.parse_url($url,PHP_URL_FRAGMENT),'',$url);
Using parse_url():
function removeURLFragment($pstr_urlAddress = '') {
$larr_urlAddress = parse_url ( $pstr_urlAddress );
return $larr_urlAddress['scheme'].'://'.(isset($larr_urlAddress['user']) ? $larr_urlAddress['user'].':'.''.$larr_urlAddress['pass'].'#' : '').$larr_urlAddress['host'].(isset($larr_urlAddress['port']) ? ':'.$larr_urlAddress['port'] : '').$larr_urlAddress['path'].(isset($larr_urlAddress['query']) ? '?'.$larr_urlAddress['query'] : '');
}