Parsing PHP Code From Text File – PHP

** This article is not the final version, the code will be improve. Nonetheless, this code’s style and principle is usable in many conditions. You can escape from the php script and pass information to the shell. In short, running multiple types of programming languages from one programming language. This is just not only usable in PHP, but in many different languages that support the eval feature. Nevertheless, this is article is not yet a final version.**

This is just a short release for my readers to not get bored while waiting for the finishing of the infinity formula. I halt the editing on the infinity formula due to the development of an automation program for advertisement distribution.

These three functions are a parser that will read in any text file or any string. When reading of the string, any code in between of {{PHP}} {{/PHP}} will be run as php code. Any others text will be echo by the echo command. There can be more than one set of {{PHP}}{{/PHP}}. I can’t go into detail for this article right at this moment, but this will change into an article that will show you in detail how the function work. The benefits for this function is you can centralize all your php code on one domain in a text file, and run it off from another server. This is good for multiple domains that are not on the same server. You can always use .htaccess file to limit which IP can access the text folder that stored the php codes. Nevertheless, this is eval code so please do understand that there is a security risk involved if the text file can be read by unauthorized personnel or can be altered by anyone.

To use the code all you have to do is have the three function together, pass any text string or text file to writer($data) and voila it will work. The php code inside the tag will be executed first before anything else is being echoed. You can modify this as desired. Nevertheless, do take security measurement when running eval from a clear readable text. If the php code inside the text file is broken, this function will exit with an error. I programmed this in PHP 7.0. That is the default result of what would happen when the php code that passed into an eval code is broken. We can always check the code by parsing it into a php parser before parsing the code to the eval() function. Just a quick reminder, text in between of {{php}} {{/php}} will be treat as php code. Do not nest the {{php}}{{/php}} tags, doing so will create an error. Please do not change the start tag and end tag, that will also create an error. If you want to customize the start tag and end tag you do need to modify the code. Where the plus 7 is, that is the string length of the start tag. The plus eight is the string length of the end tag. When choosing the start tag and end tag, it is best to choose set of word that you will never use or most likely never be used in writing or in code, otherwise, sometimes, you can grab the wrong text then the code will go in error. There must also be both the start tag and the end tag. If the set of start and end is missing one tag, the script will also go in error. In the final version of this article, I will show you how to build the function to check for both, the start tag and the end tag. Also, the php code, including the start and end tag will be removed from the input text before the text being echo by the function.

When the eval code is return, the return eval function will always return an array. Nevertheless, that can be customize to return string when one tag is found, and when there is more than one set of tag then an array would be return. Else that, everything is easy as this is just a basic demonstration of something that will be more complex when putting in real life pratical examples.

function runEval($invalue){
  if (gettype($invalue) === "string") eval($invalue);
  if (gettype($invalue) === "array"){
    for( $i = 0; $i < count($invalue); $i++){
      eval($invalue[$i]);
    }
  }
}

function returnEval($a, $starttag, $endtag){
  $tempstr = "";
  $codeArray = array();
  while( strpos($a, $starttag) || strpos($a, $endtag) ){
    $posa = 0;
    $posb = 0;
    $strlen = strlen($a);
    $posa = strpos($a, $starttag);
    $from = $posa + 7;
    $to = $strlen - $posa;
    $a = substr($a, $from, $to);

    $posb = strpos($a, $endtag);
    $strlen = strlen($a);
    $from = $posb + 8;
    $to = $strlen - $from;
    $tempstr = substr($a, 0, $posb);
    $a = substr($a, $from, $to);
    array_push($codeArray, $tempstr);
  }
  return (count($codeArray) > 0? $codeArray : 0) ;
}

function writer($data){
  $st = "{{php}}";
  $et = "{{/php}}";

  $code = returnEval($data, $st, $et);

  if($code !== 0){
  	runEval($code);
  	if (gettype($code) === "string") $data = str_replace($st . $code . $et, '', $data);
  	if (gettype($code) === "array"){
  		for( $i = 0; $i < count($code); $i++){
  			$data = str_replace($st . $code[$i] . $et, '', $data);
  		}
  	}
  }
  echo $data;
}

This code below is the update and will run the php exactly where it is. This code below will most likely be the code for this article if I ever have time to write this article.


$st = "{{php}}";
$et = "{{/php}}";
$data = 'I want to echo the word world. Let try it: {{php}}echo "world";{{/php}} 
         Cool, I just echo that. Let do something else.
         {{php}} for($i = 0; $i < 10; $i++){
                   echo $i . "\n"; 
                 }
         {{/php}} 
         Now let check to see weather I can echo something else more cool like what I look like if I can grab a variable that is my parent.
         {{php}}
			echo $parent;        
         {{/php}}         
         ';
$parent = "I am a parent variable";

function returnEval($a, $starttag, $endtag){
	$tempstr = "";
	$codeArray = array();
	while( strpos($a, $starttag) || strpos($a, $endtag) ){
		$posa = 0;
		$posb = 0;
		$strlen = strlen($a);
		$posa = strpos($a, $starttag);
		$from = $posa + 7;
		$to = $strlen - $posa;
		$a = substr($a, $from, $to);
	
		$posb = strpos($a, $endtag);
		$strlen = strlen($a);
		$from = $posb + 8;
		$to = $strlen - $from;
		$tempstr = substr($a, 0, $posb);
		$a = substr($a, $from, $to);
		array_push($codeArray, $tempstr);
	}
	return (count($codeArray) > 0? $codeArray : 0) ;
}

function runEval($invalue){
	if (gettype($invalue) === "string") eval($invalue);
	if (gettype($invalue) === "array"){
		for( $i = 0; $i < count($invalue); $i++){
			eval($invalue[$i]);
		}
	}
}

$code = returnEval($data, $st, $et);

if($code !== 0){
	if (gettype($code) === "string") $data = str_replace($st . $code . $et, "<?php " . $code . " ?>", $data);
	if (gettype($code) === "array"){
		for( $i = 0; $i < count($code); $i++){
			$data = str_replace($st . $code[$i] . $et, "<?php " . $code[$i] . " ?>", $data);
		}
	}
}



$evalStr = "?>" . $data;

eval($evalStr);
?>
Digiprove sealCopyright secured by Digiprove © 2017

This post was written by Kevin and was first post @ http://kevinhng86.iblog.website.
Original Post Name: "Parsing PHP Code From Text File – PHP".
Original Post Link: http://kevinhng86.iblog.website/2017/05/17/parsing-php-code-from-text-file-php/.

Advertisement
StudioPress Premium WordPress Themes: Foodie Pro Theme


Random Article You May Like

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*