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
or
```html
```php
echo "<h4>here is some php code</h4><p>lets see if it works</p>";
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
```php
echo.php
echo "<h4>here is some php code</h4><p>lets see if it works</p>";
test
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
$string=$Product_Details;
$delimiter="
```php
";
$delimiterto="
“;
$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("
".$arrpos[$i]."
“,‘<?php include ('.$arrpos[$i].'); ?>’,$Product_Details);
}
else
{$Product_Details = str_ireplace(”
".$arrpos[$i]."
",‘<?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
<?php echo $Product_Details;?>
The outputted html is as follows:
<?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>
But it doesn’t render any of the php, what am i doing wrong as what I am asking does it make sense?
Thanks