Echo Constant stored in DB

I have an article stored in my database, and it is already marked up in HTML.

Then I have a PHP script which serves as a template and in it is the line…

     echo $body;

What I would like to do is embed a Constant in the marked-up article in my database, and when I echo out the $body variable, the value of the Constant would appear.

Is there a way to do this?

If this was normal PHP I would do this…

     echo "<p>Welcome to " . COMPANY . ", where the customer always comes first!";

But if I have this stored in my database…

     <p>Welcome to " . COMPANY . ", where the customer always comes first!

or this…

     <p>Welcome to  COMPANY , where the customer always comes first!

…then neither works.

Hope that makes sense!

<p>Welcome to <?php echo $company; ?> where the customer always comes first!</p>

or short tage (Though I don’t like short tagging myself)

<p>Welcome to <?= $company; ?> where the customer always comes first!</p>

for not all servers recognize it and besides it really isn’t all that much more typing.

That won’t make any difference. PHP stored as a string won’t be executed, it’ll just be output as text. To execute the code, you’d need to run the string through eval(), which is dangerous and definitely not a good idea.

A better solution might be to include some sort of tokens in the string, which you could parse and replace with the actual values after you retrieve the string from the DB.

As @fretburner noted, you want your stored template to contain a token that you can replace in PHP when you echo it. As long as you know what the replacements are meant to be when you’re echo’ing the code, something like this should help you.

$dbString = "<p>Welcome to %COMPANY% where the customer always comes first!</p>";

$tokenValues = [
  "%COMPANY%" => $companyName
];

echo str_replace( array_keys($tokenValues), $tokenValues, $dbString);

Some notes on that. The % signs are there just to mark the start and end of the placeholders - this makes is easier to see where replacements are supposed to happen - there’s nothing special about the %s other than to make it less likely to replace something by accident.

Also, I used an array so that you can handle multiple replacements without having to call str_replace several times in your code. This, for example, might be relevant to want you’re hoping to achieve.

$tokenValues = [
  '%COMPANY%' => $companyName,
  '%ACCOUNT_NUMBER%' => $accountNumber,
  '%ORDER_REF%' => $orderReference
];
2 Likes

@RavenVelvet,

Thank you!!! That is a great new trick to add to my toolbelt!!

:tup:

For cases like these I prefer strtr over str_replace. strtr basically does the same thing, but it does not replace parts that were already replaced.

So, for example with str_replace

$str = 'hello';
$str = str_replace(array('hello', 'goodbye'), array('goodbye', 'hello'), $str);
echo $str; // "hello"

Here “hello” was replaced with “goodbye”, and then “goodbye” was replaced again with hello. If you use strtr, you get

$str = 'hello';
$str = strtr($str, array('hello' => 'goodbye', 'goodbye' => 'hello'));
echo $str; // "goodbye"

so first hello is replaced with goodbye, but since goodbye is already the result of a replacement it won’t be replaced again. Also, the replacements are better to read IMHO, in one associative array instead of two :slight_smile:

3 Likes

Cue Beatles song… :wink:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.