Php get url without parameters

When you have a request in hand and if the URL is having a query string with different parameters, the easiest way to get the entire URL with the query string is using the fullUrl() method like so.

$urlWithQueryString = $request->fullUrl();

// https://www.amitmerchant.com?search=Laravel&sort=asc

Now, if you want to append additional parameters in the existing URL query string, you can use the fullUrlWithQuery() like so.

$urlWithQueryString = $request->fullUrlWithQuery(['lang' => 'en']);

// https://www.amitmerchant.com?search=Laravel&sort=asc&lang=en

The new fullUrlWithoutQuery method

Now, recently with this PR, Laravel 8.x is coming with a method called fullUrlWithoutQuery() which does exactly opposite of the fullUrlWithQuery() method. i.e it removes the specified query string parameters from the request URL.

So, if we use the fullUrlWithoutQuery() the previous example, it would look like so.

$urlWithQueryString = $request->fullUrlWithoutQuery('sort');

// https://www.amitmerchant.com?search=Laravel&lang=en

As you can tell, since we specified the sort parameter in the fullUrlWithoutQuery method, it would return the URL with that parameter removed from the URL.

If you want multiple parameters to be removed, you can pass in the array of target parameters like so.

$urlWithQueryString = $request->fullUrlWithoutQuery(['sort', 'lang']);

// https://www.amitmerchant.com?search=Laravel

Beep! Beep! I'm also running a YouTube channel which I hope you're going to love!

Almost all developers at least once in their practice have wondered how to get parameters from a URL string with the help of PHP functions.

In our tutorial, we will provide you with two simple and handy functions such as pase_url() and parse_str() that will allow you to do that.

Read on and check out the options below.

Before starting, note that the parameters and the URL are separated by the ? character.

This function is aimed at returning the URL components by parsing the URL.

So, it parses the URL, returning an associative array that encompasses different components.

The syntax of the parse_url() function is as follows:

parse_url( $url, $component = -1 )

With the help of the parse_str() function, you can parse query strings into variables.

The syntax of this function is like so:

parse_str( $string, $array )

Now, let’s see examples of how to use these two functions for getting the parameters from the URL string.

<?php

// Initialize URL to the variable 
$url = 'http://w3docs.com?name=John';

// Use parse_url() function to parse the URL 
// and return an associative array which 
// contains its various components 
$url_components = parse_url($url);

// Use parse_str() function to parse the 
// string passed via URL 
parse_str($url_components['query'], $params);

// Display result 
echo ' Hi ' . $params['name'];

?>

The output of this example will be:

    Hi John

Now, let’s consider another example:

<?php

// Initialize URL to the variable 
$url = 'http://w3docs.com/register?name=Andy&[email protected]';

// Use parse_url() function to parse the URL 
// and return an associative array which 
// contains its various components 
$url_components = parse_url($url);

// Use parse_str() function to parse the 
// string passed via URL 
parse_str($url_components['query'], $params);

// Display result 
echo ' Hi ' . $params['name'] . ' your emailID is ' . $params['email'];

?>

And, the output of this example will look like this:

  Hi Andy your emailID is [email protected]

So, in this tutorial, we highlighted the two handy functions that will allow getting parameters from a URL string with PHP.

Hope, this tutorial helped to achieve your goals.

How to fetch value from URL in PHP?

The parameters from a URL string can be retrieved in PHP using parse_url() and parse_str() functions..
Note: Page URL and the parameters are separated by the ? ... .
Syntax: parse_url( $url, $component = -1 ).
Syntax: parse_str( $string, $array ).

How do I find a URL without query?

The task is to get the URL name of the page without using a query string with the help of JavaScript. replace() method: This method searches a string for a defined value, or a regular expression, and returns a new string with the replaced defined value.

How to get all parameters from URL in PHP?

Get Parameters From a URL String in PHP.
Use parse_url() and parse_str() Functions to Get Parameters From a URL String in PHP..
Use $_GET Variable to Get Parameters From a URL String in PHP..