0

With $_SERVER['REQUEST_URI'], I get a URL that could be:

index.php 

or

index.php?id=x&etc..

I'd like to do two things:

  1. Find if there is a ?something after index.php name with regular expression.

  2. If there is in the url a specific var (id=x) and delete it from the url.

For example:

index.php?id=x       => index.php 
index.php?a=11&id=x  => index.php?a=11

How can I do this?

2 Answers 2

2

To check if there is a ?something after index.php, you could use the built-in function parse_url(), like so:

if (parse_url($url, PHP_URL_QUERY)) {
    // ?something exists
}

To remove the id, you could use parse_str(), get the query parameters, store them in an array, and unset the particular id.

And since you also want to re-create the URL after the particular element is deleted from the query part of the URL, then you could use http_build_query().

Here's a function for that:

function removeQueryString($url, $toBeRemoved, $match) 
{
    // check if url has query part
    if (parse_url($url, PHP_URL_QUERY)) {

        // parse_url and store the values
        $parts = parse_url($url);
        $scriptname = $parts['path'];
        $query_part = $parts['query'];

        // parse the query parameters from the url and store it in $arr
        $query = parse_str($query_part, $arr);

        // if id == x, unset it
        if (isset($arr[$toBeRemoved]) && $arr[$toBeRemoved] == $match) {
            unset($arr[$toBeRemoved]);

            // if there less than 1 query parameter, don't add '?'
            if (count($arr) < 1) {
                $query = $scriptname . http_build_query($arr);

            } else {
                $query = $scriptname . '?' . http_build_query($arr);  
            }
        } else {
            // no matches found, so return the url
            return $url;
        }
        return $query;
    } else {
        return $url;
    }
}

Test cases:

echo removeQueryString('index.php', 'id', 'x');
echo removeQueryString('index.php?a=11&id=x', 'id', 'x');
echo removeQueryString('index.php?a=11&id=x&qid=51', 'id', 'x');
echo removeQueryString('index.php?a=11&foo=bar&id=x', 'id', 'x');

Output:

index.php
index.php?a=11
index.php?a=11&qid=51
index.php?a=11&foo=bar

Demo!

Sign up to request clarification or add additional context in comments.

1 Comment

There is a problem. I set $toBeRemoved='id', $match='12'. If url is index.php?id=12&a=10 return index.php?a=10 but if it's not match example-> index.php?id=11&a=10 return blank.
1

If it must be a regular expression :

$url='index.php?a=11&id=1234';
$pattern = '#\id=\d+#';
$url = preg_replace($pattern, '', $url);

echo $url;

output

index.php?a=11&

There is a trailing &, but the above removes any id=xxxxxxxx

2 Comments

Thank you for your answer, but I realized that it is better to use the method suggested by Amal Murali
@Paolo - I totally agree!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.