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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:36:46+00:00 2026-05-23T12:36:46+00:00

$var1 = ‘abc’; $var2 = ‘123’; How can I replace %var1 and %var2% from

  • 0
$var1 = 'abc';
$var2 = '123';

How can I replace %var1 and %var2% from a string like this:

aaaaaaaa%var1%bbbbbbbbb%var2%ffffffff

with the value of $var1 and $var2 ?

  • 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-23T12:36:47+00:00Added an answer on May 23, 2026 at 12:36 pm

    Assuming >= PHP 5.3…

    preg_replace_callback('%(\w+?)%', function($matches) use ($var1, $var2) {
       return $$matches[1][0];
    }, $str);
    

    As you can see, you need to pass a reference to each of the outer variables to the closure.

    You are probably better constructing an array with the replacement variables, and just passing that array in and then subscripting it…

    preg_replace_callback('%(\w+?)%', function($matches) use ($vars) {
       return isset($vars[$matches[1][0]]) ? $vars[$matches[1][0]] : $matches[0][0];
    }, $str);
    

    I haven’t got a chance to test this code right now, but I believe the general principle is sound 🙂

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

Sidebar

Related Questions

I need to extract in actionscript the variables from a string like /var1/var2/var3/... Each
I have a data frame with gaps like this: Var1 Var2 Var3 1 NA
I can do string=%s*3 print string %(var1,var2,var3) but I can't get the vars into
I have a JSON String like this $test='{var1:null,var3:null,status:{code:150,message:blah blah}}'; I want to access the
Like $args = ( 'var1' => 'abc', 'var2' => 'def', ); $text = sprintf('first
I have a javascript class defined like this: myClass = { var1:0, var2:0, initVars:
I have code like this: var newMsg = new Msg { Var1 = var1,
with a new array I do this: $aVal = array(); $aVal[key1][var1] = something; $aVal[key1][var2]
I have a formulat like (var1 - var2 ) + var3 I want to
Suppose you have a string: SELECT * FROM TABLE WHERE column1 = :var1 AND

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.