, ) : array parameter 1: string to be parsed parameter 2: array with parse options returns an array (number of replacements or error code , parsed string) Options: - start_tag default = '%{' - end_tag default = '}%' - delimiter_sign default = ':' - set_sign default = '0' - standalone_sign default = '.' - list_sign default = ',' - regexp_options default = 'imsU' - match_flags default = 'PREG_SET_ORDER' - match_offset default = '0' - functions_prefix default = '' - functions_suffix default = '' - classes_prefix default = '' - classes_suffix default = '' - classes_method default = 'run' - debug default = 'false' - parsers default = 'null' */ /*===[ PARSESTRING ] ========================================================*/ function parseString( $strStringToParse = null , $arrOptions = null ) { if( ! is_string( $strStringToParse ) ) { return array( -1 , $strStringToParse ); } $arrOptions = ( is_array( $arrOptions ) ) ? $arrOptions : array(); $arrOptions[ 'start_tag' ] = ( isset( $arrOptions[ 'start_tag' ] ) ) ? $arrOptions[ 'start_tag' ] : '%{'; $arrOptions[ 'end_tag' ] = ( isset( $arrOptions[ 'end_tag' ] ) ) ? $arrOptions[ 'end_tag' ] : '}%'; $arrOptions[ 'delimiter_sign' ] = ( isset( $arrOptions[ 'delimiter_sign' ] ) ) ? $arrOptions[ 'delimiter_sign' ] : ':'; $arrOptions[ 'set_sign' ] = ( isset( $arrOptions[ 'set_sign' ] ) ) ? $arrOptions[ 'set_sign' ] : '='; $arrOptions[ 'standalone_sign' ] = ( isset( $arrOptions[ 'standalone_sign' ] ) ) ? $arrOptions[ 'standalone_sign' ] : '.'; $arrOptions[ 'list_sign' ] = ( isset( $arrOptions[ 'list_sign' ] ) ) ? $arrOptions[ 'list_sign' ] : ','; $arrOptions[ 'regexp_options' ] = ( isset( $arrOptions[ 'regexp_options' ] ) ) ? $arrOptions[ 'regexp_options' ] : 'imsU'; $arrOptions[ 'match_flags' ] = ( isset( $arrOptions[ 'match_flags' ] ) ) ? $arrOptions[ 'match_flags' ] : PREG_SET_ORDER; $arrOptions[ 'match_offset' ] = ( isset( $arrOptions[ 'match_offset' ] ) ) ? $arrOptions[ 'match_offset' ] : 0; $arrOptions[ 'functions_prefix' ] = ( isset( $arrOptions[ 'functions_prefix' ] ) ) ? $arrOptions[ 'functions_prefix' ] : ''; $arrOptions[ 'functions_suffix' ] = ( isset( $arrOptions[ 'functions_suffix' ] ) ) ? $arrOptions[ 'functions_suffix' ] : ''; $arrOptions[ 'classes_prefix' ] = ( isset( $arrOptions[ 'classes_prefix' ] ) ) ? $arrOptions[ 'classes_prefix' ] : ''; $arrOptions[ 'classes_suffix' ] = ( isset( $arrOptions[ 'classes_suffix' ] ) ) ? $arrOptions[ 'classes_suffix' ] : ''; $arrOptions[ 'classes_method' ] = ( isset( $arrOptions[ 'classes_method' ] ) ) ? $arrOptions[ 'classes_method' ] : 'run'; $arrOptions[ 'debug' ] = ( isset( $arrOptions[ 'debug' ] ) ) ? $arrOptions[ 'debug' ] : false; $arrOptions[ 'parsers' ] = ( isset( $arrOptions[ 'parsers' ] ) ) ? $arrOptions[ 'parsers' ] : null; if( $arrOptions[ 'debug' ] === true ) { ini_set( 'display_errors' , 1 ); ini_set( 'display_startup_errors' , 1 ); error_reporting( E_ALL ); } $intM = preg_match_all( '/' . $arrOptions[ 'start_tag' ] . '([\s]*(\w+)[\s]*([' . $arrOptions[ 'delimiter_sign' ] . '|' . $arrOptions[ 'set_sign' ] . '|' . $arrOptions[ 'standalone_sign' ] . ']{1})[\s]*(.*))' . $arrOptions[ 'end_tag' ] . '/' . $arrOptions[ 'regexp_options' ] , $strStringToParse , $arrM , $arrOptions[ 'match_flags' ] , $arrOptions[ 'match_offset' ] ); if( $intM === false ) { return array( -2 , $strStringToParse ); } $arrVPFNC = function() { $arrVP = array(); $arrVP[ 'globals' ] = ( ( isset( $GLOBALS ) ) && ( is_array( $GLOBALS ) ) ) ? array_change_key_case( $GLOBALS , CASE_LOWER ) : array(); $arrVP[ 'env' ] = ( ( isset( $_ENV ) ) && ( is_array( $_ENV ) ) ) ? array_change_key_case( $_ENV , CASE_LOWER ) : array(); $arrVP[ 'server' ] = ( ( isset( $_SERVER ) ) && ( is_array( $_SERVER ) ) ) ? array_change_key_case( $_SERVER , CASE_LOWER ) : array(); $arrVP[ 'get' ] = ( ( isset( $_GET ) ) && ( is_array( $_GET ) ) ) ? array_change_key_case( $_GET , CASE_LOWER ) : array(); $arrVP[ 'post' ] = ( ( isset( $_POST ) ) && ( is_array( $_POST ) ) ) ? array_change_key_case( $_POST , CASE_LOWER ) : array(); $arrVP[ 'files' ] = ( ( isset( $_FILES ) ) && ( is_array( $_FILES ) ) ) ? array_change_key_case( $_FILES , CASE_LOWER ) : array(); $arrVP[ 'request' ] = ( ( isset( $_REQUEST ) ) && ( is_array( $_REQUEST ) ) ) ? array_change_key_case( $_REQUEST , CASE_LOWER ) : array(); $arrVP[ 'session' ] = ( ( isset( $_SESSION ) ) && ( is_array( $_SESSION ) ) ) ? array_change_key_case( $_SESSION , CASE_LOWER ) : array(); $arrVP[ 'cookie' ] = ( ( isset( $_COOKIE ) ) && ( is_array( $_COOKIE ) ) ) ? array_change_key_case( $_COOKIE , CASE_LOWER ) : array(); $arrVP[ 'headers' ] = headers_list(); if( is_array( $arrVP[ 'headers' ] ) ) { $arrVPHdrsT = array(); foreach( $arrVP[ 'headers' ] as $strTHVP ) { $arrTHVP = explode( ':' , $strTHVP ); $arrVPHdrsT[ strtolower( $arrTHVP[ 0 ] ) ] = trim( $arrTHVP[ 1 ] ); } $arrVP[ 'headers' ] = $arrVPHdrsT; } return $arrVP; }; $strSelfName = __FUNCTION__; $arrSpecialParsers = array( 'load' => function( $strV ) use( $strSelfName , $arrOptions ) { if( ( is_file( $strV ) ) && ( is_readable( $strV ) ) ) { return $strSelfName( file_get_contents( $strV ) , $arrOptions )[ 1 ]; } if( isset( $GLOBALS[ $strV ] ) ) { return $strSelfName( file_get_contents( $GLOBALS[ $strV ] ) , $arrOptions )[ 1 ]; } } , 'php' => function( $strV ) use( $strSelfName , $arrOptions ) { ob_start(); eval( $strV ); $strMXS = ob_get_contents(); ob_end_clean(); return $strMXS; } ); if( is_array( $arrOptions[ 'parsers' ] ) ) { foreach( $arrOptions[ 'parsers' ] as $strFunctionName => $strFunctionCode ) { $arrSpecialParsers[ trim( strtolower( $strFunctionName ) ) ] = function( $strData ) use( $strSelfName , $arrOptions , $strFunctionName , $strFunctionCode ) { ob_start(); eval( $strFunctionCode ); $strMXS = ob_get_contents(); ob_end_clean(); return $strMXS; }; } } foreach( $arrM as $arrCMS ) { $arrVP = $arrVPFNC(); $arrCMS[ 2 ] = strtolower( $arrCMS[ 2 ] ); $arrCMS[ 4 ] = trim( $arrCMS[ 4 ] ); if( isset( $arrSpecialParsers[ $arrCMS[ 2 ] ] ) ) { $strStringToParse = str_ireplace( $arrCMS[ 0 ] , $arrSpecialParsers[ $arrCMS[ 2 ] ]( $arrCMS[ 4 ] ) , $strStringToParse ); } elseif( $arrCMS[ 3 ] === $arrOptions[ 'delimiter_sign' ] ) { $strVL = strtolower( $arrCMS[ 4 ] ); $strCN = $arrOptions[ 'classes_prefix' ] . $arrCMS[ 2 ] . $arrOptions[ 'classes_suffix' ]; $strCM = $arrOptions[ 'classes_method' ]; if( isset( $arrVP[ $arrCMS[ 2 ] ][ $strVL ] ) ) { $strStringToParse = preg_replace( '/' . $arrCMS[ 0 ] . '/' . $arrOptions[ 'regexp_options' ] , $arrVP[ $arrCMS[ 2 ] ][ $strVL ] , $strStringToParse , 1 ); } elseif( function_exists( $arrOptions[ 'functions_prefix' ] . $arrCMS[ 2 ] . $arrOptions[ 'functions_suffix' ] ) ) { $strStringToParse = preg_replace( '/' . $arrCMS[ 0 ] . '/' . $arrOptions[ 'regexp_options' ] , call_user_func( $arrOptions[ 'functions_prefix' ] . $arrCMS[ 2 ] . $arrOptions[ 'functions_suffix' ] , explode( $arrOptions[ 'list_sign' ] , $arrCMS[ 4 ] ) ) , $strStringToParse , 1 ); } elseif( class_exists( $strCN ) ) { $objVC = new $strCN(); if( method_exists( $objVC , $arrOptions[ 'classes_method' ] ) ) { $strStringToParse = preg_replace( '/' . $arrCMS[ 0 ] . '/' . $arrOptions[ 'regexp_options' ] , $objVC->$strCM( explode( $arrOptions[ 'list_sign' ] , $arrCMS[ 4 ] ) ) , $strStringToParse , 1 ); } } else { continue; } } elseif( $arrCMS[ 3 ] === $arrOptions[ 'set_sign' ] ) { $strVV = $arrCMS[ 4 ]; $strVP = ''; if( stripos( $strVV , $arrOptions[ 'delimiter_sign' ] ) !== false ) { $arrVVP = explode( $arrOptions[ 'delimiter_sign' ] , $strVV ); $strVV = $arrVVP[ 0 ]; $strVP = $arrVVP[ 1 ]; } $strCN = $arrOptions[ 'classes_prefix' ] . $strVV . $arrOptions[ 'classes_suffix' ]; $strCM = $arrOptions[ 'classes_method' ]; if( isset( $arrVP[ 'globals' ][ $strVV ] ) ) { $GLOBALS[ $arrCMS[ 2 ] ] = $arrVP[ 'globals' ][ $strVV ]; } elseif( function_exists( $arrOptions[ 'functions_prefix' ] . $strVV . $arrOptions[ 'functions_suffix' ] ) ) { $GLOBALS[ $arrCMS[ 2 ] ] = call_user_func( $arrOptions[ 'functions_prefix' ] . $strVV . $arrOptions[ 'functions_suffix' ] , explode( $arrOptions[ 'list_sign' ] , $strVP ) ); } elseif( class_exists( $strCN ) ) { $objVC = new $strCN(); if( method_exists( $objVC , $arrOptions[ 'classes_method' ] ) ) { $GLOBALS[ $arrCMS[ 2 ] ] = $objVC->$strCM( explode( $arrOptions[ 'list_sign' ] , $strVP ) ); } } else { $GLOBALS[ $arrCMS[ 2 ] ] = $strVV . $strVP; } $strStringToParse = preg_replace( '/' . $arrCMS[ 0 ] . '/' . $arrOptions[ 'regexp_options' ] , '' , $strStringToParse , 1 ); } elseif( $arrCMS[ 3 ] === $arrOptions[ 'standalone_sign' ] ) { $strCN = $arrOptions[ 'classes_prefix' ] . $arrCMS[ 2 ] . $arrOptions[ 'classes_suffix' ]; $strCM = $arrOptions[ 'classes_method' ]; if( isset( $arrVP[ 'globals' ][ $arrCMS[ 2 ] ] ) ) { $strStringToParse = preg_replace( '/' . $arrCMS[ 0 ] . '/' . $arrOptions[ 'regexp_options' ] , $arrVP[ 'globals' ][ $arrCMS[ 2 ] ] , $strStringToParse , 1 ); } elseif( function_exists( $arrOptions[ 'functions_prefix' ] . $arrCMS[ 2 ] . $arrOptions[ 'functions_suffix' ] ) ) { $strStringToParse = preg_replace( '/' . $arrCMS[ 0 ] . '/' . $arrOptions[ 'regexp_options' ] , call_user_func( $arrOptions[ 'functions_prefix' ] . $arrCMS[ 2 ] . $arrOptions[ 'functions_suffix' ] ) , $strStringToParse , 1 ); } elseif( class_exists( $strCN ) ) { $objVC = new $strCN(); if( method_exists( $objVC , $arrOptions[ 'classes_method' ] ) ) { $strStringToParse = preg_replace( '/' . $arrCMS[ 0 ] . '/' . $arrOptions[ 'regexp_options' ] , $objVC->$strCM( explode( $arrOptions[ 'list_sign' ] , $arrCMS[ 4 ] ) ) , $strStringToParse , 1 ); } } else { continue; } } else { continue; } } $strStringToParse = preg_replace( '/' . $arrOptions[ 'start_tag' ] . '([\s]*(\w+)[\s]*([' . $arrOptions[ 'delimiter_sign' ] . '|' . $arrOptions[ 'set_sign' ] . '|' . $arrOptions[ 'standalone_sign' ] . ']{1})[\s]*(.*))' . $arrOptions[ 'end_tag' ] . '/' . $arrOptions[ 'regexp_options' ] , '' , $strStringToParse , -1 ); return array( $intM , $strStringToParse ); } /*===========================================================================*/ /*===[ DEMO-SECTION ] =======================================================*/ function tplfnc_getcdatefnc() { return date( 'Y-m-d' ); } class tplcls_getcdatecls { public function run() { return date( 'Y-m-d' ); } } function tplfnc_testmefnc( $mxP = null ) { return ( strlen( $mxP[ 0 ] ) > 0 ) ? implode( ',' , $mxP ) : 'Hello from function testmefnc'; } class tplcls_testmecls { public function run( $mxP = null ) { return ( strlen( $mxP[ 0 ] ) > 0 ) ? implode( ',' , $mxP ) : 'Hello from function testmecls'; } } $strTPLDemo = << %{title.}% %{subtitle.}%

Welcome to %{title.}% %{subtitle.}%


server:php_self / headers:special-header
server:php_self / get:getvalue
server:php_self / post:postvalue
cookie:cookievalue %{cookie:cookievalue}%
server:remote_addr %{server:remote_addr}%
request:getvalue %{request:getvalue}%
env:http_host %{env:http_host}%
Set variable cdate by PHP code %{php:\$GLOBALS[ 'cdate' ] = date( 'r' );}%%{cdate.}%
Set variable cdate by function call 'getcdatefnc' %{cdate=getcdatefnc}%%{cdate.}%
Set variable cdate by method call 'getcdatecls->run' %{cdate=getcdatecls}%%{cdate.}%
Standalone function call 'getcdatefnc' %{getcdatefnc.}%
Standalone method call 'getcdatecls->run' %{getcdatecls.}%
Set variable testme to 'Okay' %{testme=Okay}%%{testme.}%
Set variable testme to value from 'cdate' %{testme=cdate}%%{testme.}%
Set variable testme by function call 'testmefnc' %{testme=testmefnc}%%{testme.}%
Set variable testme by function call 'testmefnc(Hello World)' %{testme=testmefnc:Hello World}%%{testme.}%
Set variable testme by function call 'testmefnc(This,is,funny)' %{testme=testmefnc:This,is,funny}%%{testme.}%
Set variable testme by method call 'testmecls->run' %{testme=testmecls}%%{testme.}%
Set variable testme by method call 'testmecls->run(Hello World)' %{testme=testmecls:Hello World}%%{testme.}%
Set variable testme by method call 'testmecls->run(This,is,funny)' %{testme=testmecls:This,is,funny}%%{testme.}%
Standalone function call 'testmefnc' %{testmefnc.}%
Standalone function call 'testmefnc(Hello World)' %{testmefnc:Hello World}%
Standalone function call 'testmefnc(This,is,funny)' %{testmefnc:This,is,funny}%
Standalone method call 'testmecls->run' %{testmecls.}%
Standalone method call 'testmecls->run(Hello World)' %{testmecls:Hello World}%
Standalone method call 'testmecls->run(This,is,funny)' %{testmecls:This,is,funny}%
Include file '%{includemefilename.}%' %{load:includemefilename}%
Executing 'dingdong'
%{php:if( is_file( \$GLOBALS[ 'includemefilename' ] ) ) { file_put_contents( \$GLOBALS[ 'includemefilename' ] , '' , LOCK_EX ); unlink( \$GLOBALS[ 'includemefilename' ] ); } }% TPLDEMO; header( 'Special-Header: set by user, ok' ); $arrOptions = array( 'start_tag' => '%{' , 'end_tag' => '}%' , 'delimiter_sign' => ':' , 'set_sign' => '=' , 'standalone_sign' => '.' , 'list_sign' => ',' , 'regexp_options' => 'imsU' , 'match_flags' => PREG_SET_ORDER , 'match_offset' => 0 , 'functions_prefix' => 'tplfnc_' , 'functions_suffix' => '' , 'classes_prefix' => 'tplcls_' , 'classes_suffix' => '' , 'classes_method' => 'run' , 'debug' => true , 'parsers' => array( 'dingdong' => 'echo "selfname = " . $strSelfName . PHP_EOL . "Options = " . print_r( $arrOptions , true ) . PHP_EOL . "Function name = " . $strFunctionName . PHP_EOL . "Function code = " . $strFunctionCode . PHP_EOL . "Data (from template) = " . $strData;' ) ); $arrPSRV = parseString( $strTPLDemo , $arrOptions ); if( $arrPSRV[ 0 ] < 0 ) { header( 'Content-Type: text/plain' ); echo 'Error #' . $arrPSRV[ 0 ]; exit; } echo trim( $arrPSRV[ 1 ] ); /*===========================================================================*/ ?>