Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9154863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:29:52+00:00 2026-06-17T12:29:52+00:00

Want to remove p2 variable from url string, below are 3 cases if case

  • 0

Want to remove p2variable from url string, below are 3 cases if case 3 also remove ? sign.

case 1: http://www.domain.com/myscript.php?p1=xyz&p2=10&p3=ghj
result: http://www.domain.com/myscript.php?p1=xyz&p3=ghj

case 2: http://www.domain.com/myscript.php?p2=10&p3=ghj
result: http://www.domain.com/myscript.php?p3=ghj

case 3: http://www.domain.com/myscript.php?p2=10
result: http://www.domain.com/myscript.php

Want to achieve result with single preg_replace expression.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:29:53+00:00Added an answer on June 17, 2026 at 12:29 pm

    Don’t use regular expressions when dealing with URL values. It’s much easier (and safer) to handle them as a URL instead of plain text.

    This could be one way to do it:

    1. Split the url first and parse the query string
    2. Take the parameter out
    3. Rebuild the url

    The below code is an example of such an algorithm:

    // remove $qs_key from query string of $url
    // return modified url value
    function clean_url_qs($url, $qs_key)
    {
        // first split the url in two parts (at most)
        $parts = explode('?', $url, 2);
    
        // check whether query string is passed        
        if (isset($parts[1])) {
          // parse the query string into $params
          parse_str($parts[1], $params);
    
          // unset if $params contains $qs_key
          if (array_key_exists($qs_key, $params)) {
              // remove key
              unset($params[$qs_key]);
              // rebuild the url
              return $parts[0] . 
                  (count($params) ? '?' . http_build_query($params) : '');
          }
        }
        // no change required
        return $url;
    }
    

    Test code:

    echo clean_url('http://www.domain.com/myscript.php?p1=xyz&p2=10&p3=ghj', 'p2'), "\n";
    echo clean_url('http://www.domain.com/myscript.php?p2=10&p3=ghj', 'p2'), "\n";
    echo clean_url('http://www.domain.com/myscript.php?p2=10', 'p2'), "\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to remove the string code=hads1328fas& on my URL, so that BEFORE http://example.com/main/index.php?code=hads1328fas&store=food#home
I want to remove one value from comma separated string variable Eg hdnListCL ='ABC','PQR','XYZ'
I want to keep only numbers and remove all characters from a variable. For
In ubuntu bash script how to remove space from one variable string will be
ALTER TABLE variable ADD KEY somekey name I want to remove the line from
Using GNU Make I want to remove values from a variable: VAR := x.c
i want to remove the link from my header using CSS so that only
I want to remove duplicate lines from a file, without sorting the file. Example
I want to remove a key from a dictionary if it is present. I
I am retrieving a variable that contains a £ sign from another page. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.