1

I am working on a site which deals with a lot of texts in HTML, so recently I learned PHP and moved them into a MySQL database and echo the contents given a specific chapter and section. Now this works relatively fine, the HTML shows up properly. However, in some parts I have included PHP function calls, and these doesn't seem to be evaluated - rather, they get just inserted into the source code (and thus invisible for the page viewer), never doing anything. They did work when they were simple .html files.

How can I solve this?

For example, a text can look like this:

<?php printChapterName(); ?>
<p>Some text</p>
...
<?php showImage($name, $alt-text); ?>
...

This just shows the text on the page.

Edit: The functions are echo:ing some HTML.

3 Answers 3

2

This one should do what you want: (function evalInlinePHP and using it in preg_replace_callback)

 $text = "<?php printChapterName(); ?> <p>Lorem ipsum</p>";
 function printChapterName(){ echo '<h1>Chapter #1</h1>'; }

 function evalInlinePHP($m)
 {
  ob_start();
  eval($m[1]);
  return ob_get_clean();
 }

 echo preg_replace_callback("/<\?php(.*)\?>/", 'evalInlinePHP', $text);
 //prints <h1>Chapter #1</h1> <p>Lorem ipsum</p>

But please note that it is not a good very bad idea to store php function in DB and then evaluate it. You should really rethink how to store and display these texts

Sign up to request clarification or add additional context in comments.

1 Comment

The function itself is not stored in the database, just the calls. Is this still a bad idea?
1

Well, you're echoing those calls, that's why they get written and not interpreted.

I reckon you should rethink the way you're saving your text in the database. Why using showimage($name) when you can simply store an <img> tag? If the images go in the same places in all the pages (e.g. if you have an image associated to each page), make another column with the img src in your table and output the image in its place with another PHP call.

For instance, something on the lines of (this is a rough example, but you get the idea):

$res = mysql_query("SELECT text, image FROM contents WHERE id_pag = ".$id);
$row = mysql_fetch_array($res);
// Output the image
echo '<img src="'.$row['image'].'" />'. $row['text'];

5 Comments

I made the function so it would be a short hand for both centering and including the path where I would save the images, so that if I ever changed it in the future I didn't have to go through each page individually. This seems like an interesting idea but most pages don't have images, and some have two etc.
+1 for rethinking db design. storing code and/or markup in the database is a very poor decision and inherently dangerous
@pg-robban: Well, it's difficult to give you a precise solution not knowing exactly what your pages look like... but an option would be to save the images as [img]images/test.jpg[/img]. When you retrieve the text you just use preg_replace to change [img]xxx[/img] into <img src="xxx" class="centeredImg" /> or something like that.
Interesting idea. But this is not my only function call, so it only solves one of the problems.
Well, many forums use this strategy. You can make up all the tags that you like, [img], [list], [table] etc etc. It can be even more complex things, for instance on my website I use a [map long, lat, zoom] tag that shows up a google map at the requested coordinates/zoom level. It is a much safer option then using the evil eval :)
1

Use eval() so that PHP evaluates the code coming from your database. Note that this is inherently dangerous unless you're the only person who's in charge of that PHP code. Also make sure that those functions are defined in your current page.

2 Comments

Be very careful in using eval, it can lead to disaster. As I said, rethink the storage of your contents, it may take longer but you'll have a cleaner product at the end.
Eval is dangerous but in terms of answering the question then this is the right answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.