<?php
/*
parseString
===========
(C/W) CI:A - Certus In Audio [http://certus.in/]
A very, very, ..., simple function to parse strings.
Sometimes we all need a simple/basic function to parse strings.
This is one of the simplest function ever (to parse strings, indeed). ;-)
It is nasty and dirty written, but it does its duty - not very hard and
even not very strong or stable or ... on a beautiful way. But it does.
It is just a FUNCTION EXAMPLE, and maybe you will find it useful.
Feel free to use it (on your own risk, for sure).
HAVE FUN! :-D
Sourced by CI:A - Certus In Audio [http://certus.in/].
USAGE:
Please have a look at the example code below.
parseString( <string> , <array> ) : 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 = <<<TPLDEMO
%{title=parseString()}%
%{subtitle=- Demo}%
%{php:if( isset( \$_GET[ 'setheader' ] ) )
{ header( 'Special-Header: ' . urldecode( \$_GET[ 'setheader' ] ) ); }}%
%{php:setcookie( 'cookievalue' , date( 'r' ) , time() + 3600 );}%
%{php:if( is_file( \$_SERVER[ 'SCRIPT_FILENAME' ] ) )
{ \$GLOBALS[ 'includemefilename' ] = \$_SERVER[ 'SCRIPT_FILENAME' ] . '.txt';
file_put_contents( \$GLOBALS[ 'includemefilename' ] ,
'Welcome to ' . chr( 37 ) . '{title.}' . chr( 37 ) .
'! It is now: ' . date( 'r' ) . ' and you are ' .
\$_SERVER[ 'REMOTE_ADDR' ] ,
LOCK_EX );
} }%
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>%{title.}% %{subtitle.}%</title>
</head>
<body>
<h1>Welcome to <i>%{title.}%</i> %{subtitle.}%</h1>
<hr>
<table style="width:100%;">
<tr>
<td>
<code>
server:php_self</code> / <code>headers:special-header
</code>
</td>
<td>
<form action="%{server:php_self}%" method="GET">
<input type="text"
name="setheader"
value="%{headers:special-header}%"
size="20"
maxlength="1024" style="width:80%;">
<input type="submit" value="Send" style="width:10%;">
</form>
</td>
</tr>
<tr>
<td>
<code>
server:php_self</code> / <code>get:getvalue
</code>
</td>
<td>
<form action="%{server:php_self}%" method="GET">
<input type="text"
name="getvalue"
value="%{get:getvalue}%"
size="20"
maxlength="1024"
style="width:80%;">
<input type="submit" value="Send" style="width:10%;">
</form>
</td>
</tr>
<tr>
<td>
<code>server:php_self</code> / <code>post:postvalue</code>
</td>
<td>
<form action="%{server:php_self}%" method="POST">
<input type="text"
name="postvalue"
value="%{post:postvalue}%"
size="20"
maxlength="1024"
style="width:80%;">
<input type="submit" value="Send" style="width:10%;">
</form>
</td>
</tr>
<tr>
<td><code>cookie:cookievalue</code></td>
<td>%{cookie:cookievalue}%</td>
</tr>
<tr>
<td><code>server:remote_addr</code></td>
<td>%{server:remote_addr}%</td>
</tr>
<tr>
<td><code>request:getvalue</code></td>
<td>%{request:getvalue}%</td>
</tr>
<tr>
<td><code>env:http_host</code></td>
<td>%{env:http_host}%</td>
</tr>
<tr>
<td>Set variable <code>cdate</code> by PHP code</td>
<td>%{php:\$GLOBALS[ 'cdate' ] = date( 'r' );}%%{cdate.}%</td>
</tr>
<tr>
<td>
Set variable <code>cdate</code>
by function call 'getcdatefnc'
</td>
<td>%{cdate=getcdatefnc}%%{cdate.}%</td>
</tr>
<tr>
<td>
Set variable <code>cdate</code>
by method call 'getcdatecls->run'
</td>
<td>%{cdate=getcdatecls}%%{cdate.}%</td>
</tr>
<tr>
<td>Standalone function call 'getcdatefnc'</td>
<td>%{getcdatefnc.}%</td>
</tr>
<tr>
<td>Standalone method call 'getcdatecls->run'</td>
<td>%{getcdatecls.}%</td>
</tr>
<tr>
<td>Set variable <code>testme</code> to 'Okay'</td>
<td>%{testme=Okay}%%{testme.}%</td>
</tr>
<tr>
<td>Set variable <code>testme</code> to value from 'cdate'</td>
<td>%{testme=cdate}%%{testme.}%</td>
</tr>
<tr>
<td>
Set variable <code>testme</code>
by function call 'testmefnc'
</td>
<td>%{testme=testmefnc}%%{testme.}%</td>
</tr>
<tr>
<td>
Set variable <code>testme</code>
by function call 'testmefnc(Hello World)'
</td>
<td>%{testme=testmefnc:Hello World}%%{testme.}%</td>
</tr>
<tr>
<td>
Set variable <code>testme</code>
by function call 'testmefnc(This,is,funny)'
</td>
<td>%{testme=testmefnc:This,is,funny}%%{testme.}%</td>
</tr>
<tr>
<td>
Set variable <code>testme</code>
by method call 'testmecls->run'
</td>
<td>%{testme=testmecls}%%{testme.}%</td>
</tr>
<tr>
<td>
Set variable <code>testme</code>
by method call 'testmecls->run(Hello World)'
</td>
<td>%{testme=testmecls:Hello World}%%{testme.}%</td>
</tr>
<tr>
<td>
Set variable <code>testme</code>
by method call 'testmecls->run(This,is,funny)'
</td>
<td>%{testme=testmecls:This,is,funny}%%{testme.}%</td>
</tr>
<tr>
<td>Standalone function call 'testmefnc'</td>
<td>%{testmefnc.}%</td>
</tr>
<tr>
<td>Standalone function call 'testmefnc(Hello World)'</td>
<td>%{testmefnc:Hello World}%</td>
</tr>
<tr>
<td>Standalone function call 'testmefnc(This,is,funny)'</td>
<td>%{testmefnc:This,is,funny}%</td>
</tr>
<tr>
<td>Standalone method call 'testmecls->run'</td>
<td>%{testmecls.}%</td>
</tr>
<tr>
<td>Standalone method call 'testmecls->run(Hello World)'</td>
<td>%{testmecls:Hello World}%</td>
</tr>
<tr>
<td>Standalone method call 'testmecls->run(This,is,funny)'</td>
<td>%{testmecls:This,is,funny}%</td>
</tr>
<tr>
<td>Include file '%{includemefilename.}%'</td>
<td>%{load:includemefilename}%</td>
</tr>
<tr>
<td>Executing 'dingdong'</td>
<td>
<textarea cols="80"
rows="10"
style="width:90%;">%{dingdong:Hey, there! :-)}%
</textarea>
</td>
</tr>
</table>
</body>
</html>
%{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 ] );
/*===========================================================================*/
?>