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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:10:12+00:00 2026-05-19T04:10:12+00:00

Given the following: $foo = Yo [user Cobb] I heard you like dreams so

  • 0

Given the following:

$foo = “Yo [user Cobb] I heard you like dreams so I put a dream in yo dream in yo dream so you can dream while you dream while you dream.”

I’d like to do this:

$foo = bar($foo);

echo $foo;

And get something like this:

Yo Cobb I heard you like dreams so I put a dream in yo dream in yo dream so you can dream while you dream while you dream.

I’m unsure of how the bar function should work. I think this is doable with regular expressions but I personally find those hard to understand. Using the strpos function is another method but I wonder if there is a better solution.

Pseudocode is fine but actual code will be appreciated.

Edit:

These tags are not placeholders as the 2nd part is a variable value.

Edit:

All of the str_replace answers are incorrect as the tags contain variable content.

  • 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-19T04:10:12+00:00Added an answer on May 19, 2026 at 4:10 am

    You could use preg_match_all to search the string for tags.

    function bar($foo)
    {
        $count = preg_match_all("/\[(\w+?)\s(\w+?)\]/", $foo, $matches);
        if($count > 0)
        {
             for($i = 0; $i < $count; $i++)
             {
                 // $matches[0][$i] contains the entire matched string
                 // $matches[1][$i] contains the first portion (ex: user)
                 // $matches[2][$i] contains the second portion (ex: Cobb)
    
                 switch($matches[1][$i])
                 {
                     case 'user':
                         $replacement = tag_user($matches[2][$i]);
                         str_replace($matches[0][$i], $replacement, $foo);
                         break;
                 }
             }
        }
    }
    

    Now you can add more functionality by adding more cases to the switch.

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

Sidebar

Related Questions

This is just an example, but given the following model: class Foo(models.model): bar =
In F#, given the following class: type Foo() = member this.Bar<'t> (arg0:string) = ignore()
Consider an object chain like: foo.bar.baz.bing In this particular instance, I am given foo
Given, when a user requests /foo on my server, I send the following HTTP
Given the following url: http://foo.com?bar=1&wee=2 What is the quickest way to get the raw
Given the following Oracle (10g) package definition: create or replace PACKAGE foo AS bar
Given the following: Foo has_many :bars, :through => :baz and Foo accepts_nested_attributes_for :bar I
Given the following code: public struct Foo { public Foo(int bar, int baz) :
Given the following definitions Foo has_many :bars, :dependent => :nullify, :autosave => false Bar
Given the following: struct Foo { int bar() const; }; struct IsEqual : public

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.