Analysis of zuitu group purchase the template engine

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

     Contact with the soil of friends should be very familiar with   the soil template parsing function is performed by the include/function/template.php file, is through the regular replacement, capture to complete it, similar to the principle of smarty. The analysis of his regular replacement.

         With this code in the template.php file

 

 1 function __parse($tFile,$cFile) {
 2 
 3     $fileContent = false;
 4 
 5     if(!($fileContent = file_get_contents($tFile)))
 6         return false;
 7 
 8     $fileContent = preg_replace( '/^(\xef\xbb\xbf)/', '', $fileContent ); //EFBBBF   
 9     $fileContent = preg_replace("/\<\!\-\-\s*\\\$\{(.+?)\}\s*\-\-\>/ies", "__replace('<?php \\1; ?>')", $fileContent);
10     $fileContent = preg_replace("/\{(\\\$[a-zA-Z0-9_\[\]\\\ \-\'\,\%\*\/\.\(\)\>\'\"\$\x7f-\xff]+)\}/s", "<?php echo \\1; ?>", $fileContent);
11     $fileContent = preg_replace("/\\\$\{(.+?)\}/ies", "__replace('<?php echo \\1; ?>')", $fileContent);
12     $fileContent = preg_replace("/\<\!\-\-\s*\{else\s*if\s+(.+?)\}\s*\-\-\>/ies", "__replace('<?php } else if(\\1) { ?>')", $fileContent);
13     $fileContent = preg_replace("/\<\!\-\-\s*\{elif\s+(.+?)\}\s*\-\-\>/ies", "__replace('<?php } else if(\\1) { ?>')", $fileContent);
14     $fileContent = preg_replace("/\<\!\-\-\s*\{else\}\s*\-\-\>/is", "<?php } else { ?>", $fileContent);
15 
16     for($i = 0; $i <5; ++$i) {
17         $fileContent = preg_replace("/\<\!\-\-\s*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\s*\}\s*\-\-\>(.+?)\<\!\-\-\s*\{\/loop\}\s*\-\-\>/ies", "__replace('<?php if(is_array(\\1)){foreach(\\1 AS \\2=>\\3) { ?>\\4<?php }}?>')", $fileContent);
18         $fileContent = preg_replace("/\<\!\-\-\s*\{loop\s+(\S+)\s+(\S+)\s*\}\s*\-\-\>(.+?)\<\!\-\-\s*\{\/loop\}\s*\-\-\>/ies", "__replace('<?php   if(is_array(\\1)){foreach(\\1 AS \\2) { ?>\\3<?php }}?>')", $fileContent);
19         $fileContent = preg_replace("/\<\!\-\-\s*\{if\s+(.+?)\}\s*\-\-\>(.+?)\<\!\-\-\s*\{\/if\}\s*\-\-\>/ies", "__replace('<?php if(\\1){?>\\2<?php }?>')", $fileContent);
20     
21     }
22     //Add for call <!--{include othertpl}-->
23     $fileContent = preg_replace("#<!--\s*{\s*include\s+([^\{\}]+)\s*\}\s*-->#i", '<?php include template("\\1");?>', $fileContent);
24 
25     //Add value namespace
26     if(!file_put_contents($cFile,$fileContent))    
27         return false;
28     
29 
30     return true;
31 }
32 
33 function __replace($string) {
34     return str_replace('\"', '"', $string);
35 }

Now to the regular replacement of:

The eighth line

$fileContent = preg_replace( '/^(\xef\xbb\xbf)/', '', $fileContent ); 

Is to filter out the special character of utf8 file under Windows platform ï  » ¿

The ninth line

$fileContent = preg_replace("/\<\!\-\-\s*\\\$\{(.+?)\}\s*\-\-\>/ies", "__replace('<?php \\1; ?>')", $fileContent);

The rules of:

<!(0+ -- a blank character except newline) A. any character in the}0+ character -- a blank>


Combined with the modified ies model


I insensitive 


e Will replace the content as the PHP code   __replace (PHP) as the code             execution results; not only the string   equivalent to eval (&ldquo replacement content;”)


S will not say


<--${php code}-->!;   execute any code in the PHP {}

The tenth line

 $fileContent = preg_replace("/\{(\\\$[a-zA-Z0-9_\[\]\\\ \-\'\,\%\*\/\.\(\)\>\'\"\$\x7f-\xff]+)\}/s", "<?php echo \\1; ?>", $fileContent);

The HTML template file: {$code content (can contain Chinese)}    =>   <echo ? PHP; {} between content?>

eg:{$a}Corresponding<?php echo $a; ?>


To display the content

The eleventh line

$fileContent = preg_replace("/\\\$\{(.+?)\}/ies", "__replace('<?php echo \\1; ?>')", $fileContent);

A. the template code: Code}      => <PHP echo  the code??> 

eg: ${ $a}Corresponding <?php echo  $a; ?>

The twelfth line

 $fileContent = preg_replace("/\<\!\-\-\s*\{else\s*if\s+(.+?)\}\s*\-\-\>/ies", "__replace('<?php } else if(\\1) { ?>')", $fileContent);

The template in the <--  {else  if !; ‘ a’ =’ a’}-->     to<?php } else if('a'=='a') { ?>

The thirteenth line


$fileContent = preg_replace("/\<\!\-\-\s*\{elif\s+(.+?)\}\s*\-\-\>/ies", "__replace('<?php } else if(\\1) { ?>')", $fileContent);


Template   <--  {elif   !;'a'='a'}  -->   to  <?php } else if('a'='a') { ?>

The fourteenth line


$fileContent = preg_replace("/\<\!\-\-\s*\{else\}\s*\-\-\>/is", "<?php } else { ?>", $fileContent);


Template   <--  {else}  -->!;   to <?php } else { ?>

The seventeenth line

 $fileContent = preg_replace("/\<\!\-\-\s*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\s*\}\s*\-\-\>(.+?)\<\!\-\-\s*\{\/loop\}\s*\-\-\>/ies", "__replace('<?php if(is_array(\\1)){foreach(\\1 AS \\2=>\\3) { ?>\\4<?php }}?>')", $fileContent);

In the template

<!-- {loop $arr  $k  $v} -->

"For example, PHP content"

<!--{/loop} -->

After the replacement

<?php if(is_array($arr)){foreach($arr AS $k=>$v) { ?>

"For example, PHP content"

<?php }}?>


The eighteenth line and seventeenth line is almost exactly the same as the other less $k




The nineteenth line

$fileContent = preg_replace("/\<\!\-\-\s*\{if\s+(.+?)\}\s*\-\-\>(.+?)\<\!\-\-\s*\{\/if\}\s*\-\-\>/ies", "__replace('<?php if(\\1){?>\\2<?php }?>')", $fileContent);

In the template:

<!-- {if 'a' } -->

'b'

<!-- {/if}-->

After the replacement

<?php if('a' ){?>

'b'

<?php }?>


Everyone in the test piece had better build several files (template file, parse the file, the generated PHP file) to test

Looking at the earth, welcome interested friends to discuss!

Reprint please indicate the source!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Dana at October 27, 2013 - 8:53 PM