How do I replace custom "tags" in a string?

Posted by Fake Code Monkey Rashid on Stack Overflow See other posts from Stack Overflow or by Fake Code Monkey Rashid
Published on 2011-01-11T19:48:02Z Indexed on 2011/01/11 19:53 UTC
Read the original article Hit count: 325

Filed under:

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.

© Stack Overflow or respective owner

Related posts about php