echo php code from a string

User Generated

rnfgjbbq

Programming

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:

HTML Code:

[PHP]echo.php[/PHP]
or

HTML Code:

[PHP]echo "<h4>here is some php code</h4><p>lets see if it works</p>";[/PHP]
if the tag ended in .php I know to include it as a file otherwise I was planning on just evaluating the content.

$Product_Details is being set from content in my DB.

Example string from the DB:

HTML Code:

[PHP]echo.php[/PHP][PHP]echo "<h4>here is some php code</h4><p>lets see if it works</p>";[/PHP]<br /><p>&nbsp;</p><p><strong>test</strong></p>
I have various functions as follows to help me find all the tags within a string and return them as an array for me to then replace them with the php code:


HTML Code:

        $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;

}
Then I was simply outputting as follows:

PHP Code:

<?php echo $Product_Details;?>

The outputted html is as follows:

HTML Code:

<?php include (echo.php); ?><?php include (echo "<h4>here is some php code</h4><p>lets see if it works</p>";); ?><br /><p>&nbsp;</p><p><strong>test</strong></p>
But it doesn't render any of the php, what am i doing wrong as what I am asking does it make sense?

Thanks

User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

oops sorry


Anonymous
Just what I needed…Fantastic!

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags