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 7018109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:00:04+00:00 2026-05-27T23:00:04+00:00

I would like to strip the leading slash and the querystring from a URL

  • 0

I would like to strip the leading slash and the querystring from a URL but can’t work out how to do them both. I have this code which works perfectly for stripping the querystring, but it leaves the leading slash

preg_replace('/\?.*$/', '', $_SERVER['REQUEST_URI'])

If my URL is www.mysite.com/myPage?querystring=123, the above leaves me with /myPage. How can I tweak this so I can remove the leading slash too?

Also, can you point me at a resource to help me understand preg_replace pattern matching please?

  • 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-05-27T23:00:05+00:00Added an answer on May 27, 2026 at 11:00 pm

    I might favor PHP’s simple string functions over regex in such a simple case:

    $addr  = ltrim($_SERVER['REQUEST_URI'], '/'); // zap a leading slash
    $q_pos = strpos($addr, '?'); // get the string position of a '?'
    $addr  = $q_pos !== FALSE ? substr($addr, 0, $q_pos) : $addr; // pull out URI
    

    PHP’s strpos()docs returns an integer value, so it’s possible that $q_pos === 0 … this is why we check $q_pos !== FALSE.

    UPDATE

    I suppose I should answer the question, though … so to actually use a regex in this situation …

    $address = '/test/url/test.php?extra';
    $pattern = '{^/?([^\?]+)\?.*$}';
    $replace = '$1';
    $address = preg_replace($pattern, $replace, $address);
    echo "$address"; // outputs: test/url/test.php
    

    How does this work? Well … our pattern specifies a capture group ([^\?]+) using the parentheses that grabs everything after an optional forward slash /? up to the first occurrence of an optional \? in the string. Note that we escape the actual question mark character with a backslash because it has meaning in the context of regex patterns. The final part of the regex pattern .* simply matches zero or more characters out to the end of the string.

    Finally, our replacement simply specifies the $1 to reference the text we captured with our original parentheses grouping ([^\?]+).

    One other thing to note that regex novices often fail to realize is that you aren’t required to use / as pattern delimiters. In a case like this where we’re matching actual forward slash characters I use something else (like the curly braces).

    I usually point regex beginners to this link to help them get started.

    UPDATE 2

    The regex above assumes that there is always going to be a query string, so if you run up against a URI that doesn’t have one, (for example, /All-Products), that regex won’t work. To account for this, simply alter your pattern to make the query string optional:

    $pattern = '{^/?([^\?]+)(?:\?.*)?$}'; // use an optional non-capturing group
    

    -or-

    $pattern = '{^/?([^\?]+)\??.*$}'; // make the escaped ? optional
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to strip out non-numeric elements from the POST data before using
I have a regex email pattern and would like to strip all but pattern-matched
Given a string, I would like to strip it, but I want to have
I have CharSequence source, int start, int end I would like to strip all
I would like to strip a string to have only numeric values and one
I'm trying to create a slug so I would like to strip out every
I would like to strip all img tags from a certain text, except for
I would like to strip 's (apostrophe s) from a string. For example: a=it's
I would like a brief and easy way to strip tags from an XHTML
I would like to know how to modify the below code to strip =20

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.