0

I have a blog that I manage that is mostly plain text with some HTML in it. When retrieving the text for that blog for display, I'm wanting to execute a line of PHP code within that text while hopefully avoiding the placement of PHP code in my database. I use MySQL.

In most cases, the PHP code would be something like <? include "phpfile.php"; ?>. I would be using it to insert enhancements such as polls, slideshows, etc.

In the past, I've relied on iframes to hold the PHP file.

This is the blog text. <a id='marker'></a> 
This is the second part of the blog text.

I've used "preg_replace()" to replace my marker (<a id='marker'></a>) with the code for the iframe. This yields the following, which I echo with PHP to my page:

This is the blog text. <iframe src='phpfile.php'></iframe>
This is the second part of the blog text.

This works fine, but I'd like to be able to work without the constraint of having to guess the height and width of the iframe for the content that it holds.

In reading other threads/questions, I have considered these:

  • Inserting the PHP code into my database after the user has entered his blog (bad idea)

  • Using "eval" to somehow execute my PHP code after I replace my "marker" with the code? I'm not sure how I would do this in one step. I assume I would have to do something like the next approach. (I take it that "eval()" should be avoided as much as possible!!!)

  • Breaking the blog text into two parts (held in variables). I could do consecutive "echo" statements (one for each blog part) separated by an include "phpfile.php";. I'm not sure how memory intensive this is for my server - I'd be dealing with, say, ~50kb blocks of text.

Apart from that last approach, I feel like there's got to be a simpler way to insert the PHP code and execute it.

tia for any suggestions.

broncozr
  • 117
  • 1
  • 11
  • is this a CMS you wrote? –  Aug 15 '12 at 21:59
  • What do you want to do? running a PHP code that came from user input? am I right? Or you want to run some PHP codes when user inserted a specific text in its input? – Ariyan Aug 15 '12 at 22:02
  • Yes, this is a CMS that I wrote. The blog is input by a trusted user. I will take his blog (just text) and insert my PHP code (not user-input PHP) in the middle. I would like to do this on the fly when the blog is requested by the page. Hope that makes sense. – broncozr Aug 15 '12 at 23:23

1 Answers1

0

You can simply use str_replace to solve this:

Your text:

Hello world, i want [phpcode] here

Script:

<?php $text = str_replace( "[phpcode]", phpcode(), $text);

Then define phpcode() in your code and do whatever you want.

You can also alter this by using a regular expression, and replace the text between "[ ]" by a function name:

Hello world, i want [php:functionName] here

Green Black
  • 5,037
  • 1
  • 17
  • 29
  • I tried that code, and it worked at executing the function. My "phpcode()" function just tells PHP to include my PHP file. When I echo "$text", the function runs first, and then the first and second halves of the original text are echoed. Could it be that I need to use something like preg_replace_callback()? I've read some of those questions, and I wonder if it would work? [preg_replace_callback() question](http://stackoverflow.com/questions/11537221/php-run-function-at-certain-places-preg-callback?rq=1) – broncozr Aug 16 '12 at 19:15