Replacing blocks

Hello everyone,

The content for the pages on my site are stored as text in a MySQL database. I have a rudimentary editor in my admin section that allows me to edit the HTML. For instance,

<p>This is my content</p>
<p>... and this is more content</p>

The above block is saved as a field called ‘content’ in my ‘pages’ table.

I would like to add a PHP code snippet into the middle of this code. For instance,

<p>This is my content</p>
<p>Next tuesday will be <?php echo date( "Y-m-d", strtotime("next Tuesday")); ?>.</p>
<p>... and this is more content</p>

Here’s what I was thinking…
Save the following in my database…

<p>This is my content</p>
<p>Next tuesday will be [~Next Tuesday~]
<p>... and this is more content</p>

Then use the following code to display the text properly

echo str_replace("[~Next Tuesday~]", date( "Y-m-d", strtotime("next Tuesday")), $result['content'])

Any thoughts about making this better/more efficient?

Your solution is fine, I would do the same. Also str_replace() can have each arguments as an array, so if you have multiple entries that need to be replaced with dynamic content, it is easy to implement in one function call.

Sponsor our Newsletter | Privacy Policy | Terms of Service