if ($node instanceof Text) {
$this->php[] = $this->echo(str_replace('\'', '\\\'', $node->data));
}
$data = [
(object) array(
'string' => '
CHECK
',
'inQuotes' => true,
),
(object) array(
'string' => '1 + 3 . \'-\'. 4 . \'=\' . 0',
'inQuotes' => false,
),
(object) array(
'string' => '
',
'inQuotes' => true,
),
(object) array(
'string' => '<h2>',
'inQuotes' => true,
),
(object) array(
'string' => 'Olololo',
'inQuotes' => true,
),
(object) array(
'string' => '</h2>',
'inQuotes' => true,
),
(object) array(
'string' => '
',
'inQuotes' => true,
),
(object) array(
'string' => '<h2>',
'inQuotes' => true,
),
(object) array(
'string' => 'Number is, ',
'inQuotes' => true,
),
(object) array(
'string' => 'count(["1","2","3"])',
'inQuotes' => false,
),
(object) array(
'string' => '</h2>',
'inQuotes' => true,
),
(object) array(
'string' => '
',
'inQuotes' => true,
),
'
if (rand(0,1)){',
(object) array(
'string' => '
lol
',
'inQuotes' => true,
),
'
}',
];
$res = '';
$openEcho = false;
$inQuotes = false;
foreach ($data as $str) {
if (is_string($str)) {
if ($openEcho) {
if ($inQuotes) {
$res .= "'";
}
$res .= ";";
$openEcho = false;
}
$res .= $str;
continue;
}
if (!$openEcho) {
$openEcho = true;
$inQuotes = $str->inQuotes;
$res .= "\necho ";
if ($str->inQuotes) {
$res .= "'";
}
$res .= $str->string;
} elseif ($str->inQuotes) {
if (!$inQuotes) {
$res .= ",'";
$inQuotes = true;
}
$res .= $str->string;
} elseif (!$str->inQuotes) {
if ($inQuotes) {
$res .= "'";
$inQuotes = false;
}
$res .= ",$str->string";
}
}
echo $res;
echo '
CHECK
',1 + 3 . '-'. 4 . '=' . 0,'
<h2>Olololo</h2>
<h2>Number is, ',count(["1","2","3"]),'</h2>
';
if (rand(0,1)){
echo '
lol
';
}
array (
0 =>
(object) array(
'string' => '
CHECK
',
'inQuotes' => true,
),
1 =>
(object) array(
'string' => '1 + 3 . \'-\'. 4 . \'=\' . 0',
'inQuotes' => false,
),
2 =>
(object) array(
'string' => '
',
'inQuotes' => true,
),
3 =>
(object) array(
'string' => '<h2>',
'inQuotes' => true,
),
4 =>
(object) array(
'string' => 'Olololo',
'inQuotes' => true,
),
5 =>
(object) array(
'string' => '</h2>',
'inQuotes' => true,
),
6 =>
(object) array(
'string' => '
',
'inQuotes' => true,
),
7 =>
(object) array(
'string' => '<h2>',
'inQuotes' => true,
),
8 =>
(object) array(
'string' => 'Number is, ',
'inQuotes' => true,
),
9 =>
(object) array(
'string' => 'count()',
'inQuotes' => false,
),
10 =>
(object) array(
'string' => '</h2>',
'inQuotes' => true,
),
11 =>
(object) array(
'string' => '
',
'inQuotes' => true,
),
12 => '
if (rand(0,1)){',
13 =>
(object) array(
'string' => '
lol
',
'inQuotes' => true,
),
14 => '
}',
)
public function compile($node)
{
if ($node instanceof ForeachBlock) {
$res = "";
if ($node->fallback) {
$res .= "\nif($node->expression) {\n";
}
$res .= "\nforeach ($node->expression as $node->context) {";
$res .= $this->compile($node->body);
$res .= "\n}";
if ($node->fallback) {
$res .= "\n} else {";
$res .= $this->compile($node->fallback);
$res .= "\n}";
}
return $res;
}
if ($node instanceof Text) {
return $this->echo(str_replace('\'', '\\\'', $node->data));
}
if ($node instanceof ExpressionTag) {
return $this->echo($node->expression, inQuotes: false);
}
}
$phpCode = explode(";\necho ", $phpCode);
$phpCode = implode(',', $phpCode);
<?php
echo '
CHECK
',1 + 3 . '-'. 4 . '=' . 0,'
',count(["1","2","3"]),'
',rand(0,1),'
','<h2>','Olololo','</h2>','
','<h3>','QWErt\'y','</h3>','
','<h2>','Hello, ',$name,'</h2>','
';
foreach ([1,2,3] as $item) {
echo '
','<h2>','Number is: ',$item,'</h2>','
';
}
echo '<h2>';
echo 'Number is: ';
echo count(["1","2","3"]);
echo '</h2>';
$isEchoOpen = false;
if(token === echo) {
if($isEchoOpen) {
$res.= ',';
} else {
$isEchoOpen = true;
$res .= 'echo ';
}
} else {
if($isEchoOpen) {
$isEchoOpen = false;
$res.= ';';
}
}