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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:30:01+00:00 2026-06-13T01:30:01+00:00

I have one sentence like this: {pattern} test {pattern} how r u {pattern} How

  • 0

I have one sentence like this:

{pattern} test {pattern} how r u {pattern}

How can I replace {pattern} with different values like

{AAA} test {BBB} how r u {CCC}
  • 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-13T01:30:01+00:00Added an answer on June 13, 2026 at 1:30 am

    If you wish to replace the same pattern with something else every time you could consider using preg_replace_callback(). At every match, a function is executed and you can return a different string at every invocation:

    $s = '{pattern} test {pattern} how r u {pattern}';
    
    // this gets called at every match    
    function replace_pattern($match)
    {
        // list of replacement strings $a and a looping counter $i
        static $a = array('AAA', 'BBB', 'CCC');
        static $i = 0;
    
        // return current replacement string and increase counter
        return $a[$i++ % count($a)];
    }
    
    echo preg_replace_callback('/{pattern}/', 'replace_pattern', $s);
    

    This solution cycles the replacement strings, so it will replace like AAA, BBB, CCC, AAA (again), etc. The exact strategy you wish to adopt may be different.

    The second parameter to preg_replace_callback() may also be a closure (>= 5.3)

    Also, instead of using a regular function with static declarations, it might be more appropriate to use an object for state management.

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

Sidebar

Related Questions

I have a sentence like this one: [FindThis|foo|bar] with some text between [FindThis|foo|bar]. [FindThis|foo|bar]
i have one doubt To use this sentence: filterContext.RouteData.Values[MyRouteValue] the key should be declared
This may be a silly question but... Say you have a sentence like: The
I have a string like this: inputString = this is the first sentence in
I've got a bunch of files that have sentences ending like this: \@.Next sentence
I have a sentence like this (in Hebrew,RTL): ואמר was here אוראל Now, when
I have different sentences which all have double quotes in them, like: <h3 class=myClass>Sentence
I have a sentence like 'This is [[a captured group]].' The number of words
It's hard to summarize this question into one sentence. I have two tables -
have one time consuming step that flattens a bunch of files. basically i'd like

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.