Parsing PHP code that's stored in a database.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jay
Forum Newbie
Posts: 22
Joined: Fri Jul 12, 2002 8:36 am
Location: KUL, Malaysia

Parsing PHP code that's stored in a database.

Post by Jay »

I am stuck with trying to parse PHP codes that are stored together with text in a database. For example, 1 column in my db stores text like this:

Code: Select all

This is just text. <?php echo " This WAS PHP. "; ?>This is text again.
Anybody know how I can get the PHP above to work when i retrieve the data? Is this even possible? :)
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Very possible.

Use some regexps to parse out the PHP code into a string on its own without the <? and ?>. Then use the eval() function on that string.

More info here: http://www.php.net/manual/en/function.eval.php

Good luck,
Aaron
Jay
Forum Newbie
Posts: 22
Joined: Fri Jul 12, 2002 8:36 am
Location: KUL, Malaysia

Post by Jay »

excellent, i had forgotten all about eval()... thanks!
Post Reply