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

  • Home
  • SEARCH
  • 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 3619936
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:56:28+00:00 2026-05-18T22:56:28+00:00

Basically I am trying to do an str_replace with ?cat=(insert number here) $queryString2 =

  • 0

Basically I am trying to do an str_replace with “?cat=(insert number here)”

$queryString2 = str_replace("cat=(insert number here)", "cat=4", $queryString);

Is there a way this can be accomplished? Str_replace + one character because the value im searching for could be anything.

?cat=7
?cat=3
?cat=4

Any suggestions?

  • 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-18T22:56:29+00:00Added an answer on May 18, 2026 at 10:56 pm

    A more reliable way of handling query strings would be to actually parse them.

    // If your original query string was just the data in $_GET, clone $_GET:
    $new_query = $_GET;
    // Otherwise, parse the original query string using parse_str:
    parse_str($original_query_string, $new_query);
    // Then, set the new cat value, and build a new query string.
    $new_query['cat'] = 4;
    $new_query_string = http_build_query($new_query);
    

    The technique you originally describe is the job of a regular expression 🙂

    $queryString2 = preg_replace('/cat=[0-9]+/', 'cat=4', $queryString);
    

    The regular expression cat=[0-9]+ matches the string cat= followed by one or more (+) digits ([0-9]). preg_replace replaces all matches of the regular expression (argument 1) found in the original string (argument 3) with the replacement string (argument 2) and returns the result.

    Note that this will also replace dog_and_cat=1 with dog_and_cat=4. borkweb’s answer is a more complex regex, but handles that edge case if it could arise (e.g. this is a query string provided by the user).

    I prefer the actual query parsing, but the regular expression solution should ideally work just as well, assuming no edge cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm basically trying to figure out the simplest way to perform your basic insert
I am trying to understand this one line of code below: str_replace('../', '', $route);
I recently came across this in some code - basically someone trying to create
Basically I am trying to restart a service from a php web page. Here
Im trying do this basically: var tr = document.createElement(tr); var td = document.createElement(td); td.appendChild(document.createTextNode('<input
I'm trying to streamline this method down. It basically takes in an action (string),
I'm basically trying to do this (pseudo code, not valid python): limit = 10
I'm basically trying to do this , but I don't know what T will
I'm basically trying to teach myself how to code and I want to follow
Basically I'm trying to accomplish the same thing that mailto:bgates@microsoft.com does in Internet Explorer

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.