<?php
// Como achar uma string baseando-se em um grupo de expressões regulares
// https://pt.stackoverflow.com/q/207089/53463
function analisar( $string ) {
$regex = '/
(?P<var> {{\s*\$(.*?)\s*}} )
|
(?P<loop> @for\((.*?)\)\s*{{((?:[^{}]|(?R))*)}} )
|
(?P<statement> @if\((.*?)\)\s*{{((?:[^{}]|(?R))*)}} )
/x';
$regex,
function ($matches) {
/*
//debug
echo "\n\nSubst: $matches[0]\n\$matches = ";
var_export($matches);
*/
if ($matches['var']) {
return getVar($matches[2]);
} else if ($matches['loop']) {
return loop($matches[4], $matches[5]);
} else if ($matches['statement']) {
return getStatementResult($matches[7], $matches[8]);
}
},
$string
);
return $resultado;
}
function getVar($nomeDaVariavel){
return 'VAR('
. analisar($nomeDaVariavel)
. ')';
}
function loop($nomeDoArray, $codigo){
return "LOOP\nLOOP-COND("
. analisar($nomeDoArray)
. ")\nLOOP-CODIGO("
. analisar($codigo)
. ')';
}
function getStatementResult($expressãoBooleana, $codigo){
return "IF\nIF-COND("
. analisar($expressãoBooleana)
. ")\nIF-CODIGO("
. analisar($codigo)
. ')';
}
// -----
// TESTE
// -----
$string = '
<span>{{ $nomeCompleto }}</span>
@for($nomes as $nome)
{{
@if($nome == \'Eleandro\')
{{
<p>{{ $nome }}</p>
}}
}}
';
echo analisar($string);
PD9waHAKCi8vIENvbW8gYWNoYXIgdW1hIHN0cmluZyBiYXNlYW5kby1zZSBlbSB1bSBncnVwbyBkZSBleHByZXNzw7VlcyByZWd1bGFyZXMKLy8gIGh0dHBzOi8vcHQuc3RhY2tvdmVyZmxvdy5jb20vcS8yMDcwODkvNTM0NjMKCmZ1bmN0aW9uIGFuYWxpc2FyKCAkc3RyaW5nICkgewoJJHJlZ2V4ID0gJy8KCSAgICAgICAgCQkoP1A8dmFyPiAgICAgICB7e1xzKlwkKC4qPylccyp9fSAgICAgICAgICAgICAgICAgICAgICApCgkgICAgICAgIAl8CgkJCQkJKD9QPGxvb3A+ICAgICAgQGZvclwoKC4qPylcKVxzKnt7KCg/Oltee31dfCg/UikpKil9fSAgKQoJCQkJfAoJCQkJCSg/UDxzdGF0ZW1lbnQ+IEBpZlwoKC4qPylcKVxzKnt7KCg/Oltee31dfCg/UikpKil9fSAgICkKCQkJCS94JzsKCQoJJHJlc3VsdGFkbyA9IHByZWdfcmVwbGFjZV9jYWxsYmFjaygKCSAgICAgICAgICAgICAgICAgJHJlZ2V4LAoJICAgICAgICAgICAgICAgICBmdW5jdGlvbiAoJG1hdGNoZXMpIHsKCSAgICAgICAgICAgIAkvKgoJICAgICAgICAgICAgCQkvL2RlYnVnCgkgICAgICAgICAgICAgICAgIAkJZWNobyAiXG5cblN1YnN0OiAkbWF0Y2hlc1swXVxuXCRtYXRjaGVzID0gIjsKCSAgICAgICAgICAgICAgICAgCQl2YXJfZXhwb3J0KCRtYXRjaGVzKTsKCSAgICAgICAgICAgICAgICAgCSovCgkgICAgICAgICAgICAgICAgICAgICBpZiAoJG1hdGNoZXNbJ3ZhciddKSB7CgkgICAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIGdldFZhcigkbWF0Y2hlc1syXSk7CgkKCSAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAoJG1hdGNoZXNbJ2xvb3AnXSkgewoJICAgICAgICAgICAgICAgICAgICAgICAgIHJldHVybiBsb29wKCRtYXRjaGVzWzRdLCAkbWF0Y2hlc1s1XSk7CgkKCSAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAoJG1hdGNoZXNbJ3N0YXRlbWVudCddKSB7CgkgICAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIGdldFN0YXRlbWVudFJlc3VsdCgkbWF0Y2hlc1s3XSwgJG1hdGNoZXNbOF0pOwoJICAgICAgICAgICAgICAgICAgICAgfQoJICAgICAgICAgICAgICAgICB9LAoJICAgICAgICAgICAgICAgICAkc3RyaW5nCgkgICAgICAgICAgICAgKTsKCglyZXR1cm4gJHJlc3VsdGFkbzsKfQoKZnVuY3Rpb24gZ2V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