Description
I have a cms system where a client has requested that they be able to enter php snippets within the html content.
This would be in the following format:
[PHP]echo.php[/PHP]
[PHP]echo "<h4>here is some php code</h4><p>lets see if it works</p>";[/PHP]
$Product_Details is being set from content in my DB.
Example string from the DB:
[PHP]echo.php[/PHP][PHP]echo "<h4>here is some php code</h4><p>lets see if it works</p>";[/PHP]<br /><p> </p><p><strong>test</strong></p>
$string=$Product_Details; $delimiter="[php]"; $delimiterto="[/php]"; $arrpos=array(); $arrpos=getSelectiveContent($string,$delimiter,$delimiterto,$exclude=""); $arrsize=sizeof($arrpos); for($i=0; $i<$arrsize; $i++) { if(stripos($arrpos[$i],".php")>=0){ $Product_Details = str_ireplace("[php]".$arrpos[$i]."[/php]",'<?php include ('.$arrpos[$i].'); ?>',$Product_Details); } else {$Product_Details = str_ireplace("[php]".$arrpos[$i]."[/php]",'<?php echo eval('.$arrpos[$i].'); ?>',$Product_Details);} } function getSelectiveContent($content,$from,$to,$exclude="") { $return = array(); // array for return elements $size_FROM = strlen($from); $size_TO = strlen($to); while(true) { $pos = stripos($content,$from); // find first occurance of $from if( $pos === false ) { break; // if not exist break loop } else { $element = extractor($content,$from,$to); // fetch first element if($exclude == "") { if( trim($element) != "" ) { $return[] = trim($element); } } else { if(trim($element) != "" && !strstr($element,$exclude)) // if nothing in range, and exclude is not in it { $return[] = trim($element); // put fetched content in array. } } $content = substr($content,$pos+strlen($element)+$size_FROM+$size_TO); // remove $from to $to from content } } unset($content,$from,$to,$element,$exclude,$pos,$size_FROM,$size_TO); return $return; } function extractor($str,$afrom,$ato) { $from_pos = stripos($str,$afrom); $from_pos = $from_pos + strlen($afrom); $to_pos = stripos($str,$ato,$from_pos);// to must be after from $return = substr($str,$from_pos,$to_pos-$from_pos); unset($str,$afrom,$ato,$from_pos,$to_pos ); return $return; }
<?php echo $Product_Details;?>
<?php include (echo.php); ?><?php include (echo "<h4>here is some php code</h4><p>lets see if it works</p>";); ?><br /><p> </p><p><strong>test</strong></p>
Thanks