1

I have a nested variable in my mysql column, now printing it out, I would like it to also being parsed within my script but am not sure how to do it, I even tried doing double $$id and it still didn't work.

Here's what I have:

Pulled from mysql, $message now has = Your order $$id is not ready yet.

$id="234"; echo $message; 

prints:

Your order $$id is not ready yet. 

really want it to show:

Your order 234 is not ready yet. 

Any kind of help I can get on this is greatly appreciated!

4
  • @habibulhaq $message prints Your order $$id is not ready yet. Commented Apr 30, 2015 at 6:30
  • 1
    what he means is, how is message defined? Commented Apr 30, 2015 at 6:33
  • I hate to say eval Commented Apr 30, 2015 at 6:33
  • no need to eval, if there's a placeholder in $message, it just needs to be replaced Commented Apr 30, 2015 at 6:34

1 Answer 1

1

Since $message is coming from a database, it should look like the below, in which case you can do the following.

 $message = 'Your order $$id is not ready yet'; $id = "234"; $message = str_replace('$$id', $id, $message); print $message; 

For completeness - if your strings, and your replacement values were in an array - you could make it a little smoother:

 $strings = array( 'Your order $$id is not ready yet', 'Thank you $$name for your order', ); $replacements = array( 'id' => "234", 'name' => "thevoipman", ); foreach($strings as &$message) { foreach($replacements as $placeholder => $value) { $message = str_replace('$$' . $placeholder, $value, $message); } } print_r($strings); 
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, but I was hoping there's a quicker way not using str_replace...... I did a mysql select to get $message
@thevoipman You need to replace it, it's one additional line - and i updated to match your message format
@thevoipman i've added an example of how you might be able to approach doing it in a less repetitive manner.
@thevoipman There's no real way around that. Even if you don't write it explicitly in PHP, it will happen behind the scenes.
@thevoipman then you have to ask yourself what is more work - refactoring EVERYTHING to make this easier, or just adding 10x str_replace
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.