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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:23:16+00:00 2026-06-06T09:23:16+00:00

I am trying to replace $1, $2, $3 variables in a URL with another

  • 0

I am trying to replace $1, $2, $3 variables in a URL with another URL.
You can copy paste my example below and see my solution.

But I feel like there is a more elegant way with an array mapping type function or a better preg_replace type of thing. I just need a kick in the right direction, can you help?

    <?php
    /**
    *  Key = The DESIRED string
    *  Value = The ORIGINAL value
    *
    * Desired Result: project/EXAMPLE/something/OTHER
    */
    $data = array(
        'project/$1/details/$2' => 'newby/EXAMPLE/something/OTHER'
    );

    foreach($data as $desiredString => $findMe)
    {
        /**
        * Turn these URI's into arrays
        */
        $desiredString = explode('/', $desiredString);
        $findMe = explode('/', $findMe);

        /**
        * Store the array position of the match
        */
        $positions = array();
        foreach($desiredString as $key => $value) {
            /**
            * Look for $1, $2, $3, etc..
            */
            if (preg_match('#(\$\d)#', $value)) {
                $positions[$key] = $value;
            }
        }

        /**
        * Loop through the positions
        */
        foreach($positions as $key => $value){
            $desiredString[$key] = $findMe[$key];
        }

        /**
        * The final result
        */
        echo implode('/', $desiredString);
    }
  • 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-06T09:23:17+00:00Added an answer on June 6, 2026 at 9:23 am

    Just saying, why don’t use an array pattern/replacement in preg_replace? Something like this:

    <?php
    /**
    *  Key = The DESIRED string
    *  Value = The ORIGINAL value
    *
    * Desired Result: project/EXAMPLE/something/OTHER
    */
    $data = array(
        'project/$1/details/$2' => 'newby/EXAMPLE/details/OTHER'
    );
    $string = 'project/$1/details/$2';
    
    $pattern[0] = '/\$1/';
    $pattern[1] = '/\$2/';
    $replacement[0] = 'EXAMPLE';
    $replacement[1] = 'OTHER';
    $result = preg_replace($pattern, $replacement, $string);
    
    echo $result;
    

    I think that it’s much easier than what you’re looking for. You can see that it works here: http://codepad.org/rCslRmgs

    Perhaps there’s some reason to keep the array key => value to accomplish the replace?

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

Sidebar

Related Questions

I am trying to replace some mathematical operators with variables, so that I can
Trying to replace any non alpha-numeric characters with a hyphen. Can't see why it
I am trying to replace a large string in groovy. But can't get it
I am trying to write a bash script(script.sh) to search and replace some variables
I am trying to do a search and replace with variables. In this case
I'm trying to do a URL GET variable replace, however the regular expression for
Is there any PHP PDF library that can replace placeholder variables in an existing
I am trying to work out the best way to replace multiple variables/placeholders within
I am trying to build a regular expression to replace unresolved velocity variables with
So, I am trying to make a tabs menu, like this: http://flowplayer.org/tools/tabs/index.html (but 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.