Evil Eval!

Show off new creations, get help, or just chat about all aspects of web development and design.

Evil Eval!

Postby Berusadeankan on Tue May 05, 2009 12:36 am

The php function eval() is evil, we all know that.

I need some help getting this script to run through my entire string and not stop on the first [ code ] tag.
Code: Select all
$text = "<h1>PHP code incoming!</h1>
[code]\$testvar = \"Hejsan lol!<br /><br />\";
echo \"test<br />\";
echo \$testvar;[/code]
End of that! But wait?! :D
[code] echo \"MORE CODE!\"; [/code]";

$phpSegment = explode("[code]", $text);
$htmlSegment1 = $phpSegment[0];
$phpSegment = explode("[/code]", $phpSegment[1]);
$phpSegmentCode = $phpSegment[0];
$htmlSegment2 = $phpSegment[1];

echo $htmlSegment1;
echo eval("$phpSegmentCode");
echo $htmlSegment2;
Berusadeankan
Regular
Regular
 
Joined: Sun Jan 02, 2005 6:21 am

Re: Evil Eval!

Postby zombie@computer on Tue May 05, 2009 7:57 am

Dont use explode, use preg_split

Something like $bits = preg_split( "/\\[\\/?code\\]/", $text); (not tested)
When you are up to your neck in shit, keep your head up high
zombie@computer
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Fri Dec 31, 2004 5:58 pm
Location: Lent, Netherlands

Re: Evil Eval!

Postby blackdeath on Fri May 08, 2009 10:26 pm

he's right. explode isn't powerful enough without subjecting it to more crap than would be necessary if you didn't just use regexps.

regexps are your friend.
blackdeath
Regular
Regular
 
Joined: Sat Mar 07, 2009 10:40 pm

Return to Web Design & Development

Who is online

Users browsing this forum: No registered users

cron