Laravel - 带有刀片compileString的php eval

我正在尝试将刀片模板字符串解析为php格式并使用 eval()</ strong >将字符串评估为php代码</ p>

  $ array = [
'foo'=&gt; 'bar',
'bar'=&gt; 'foo'
$ content ='@ foreach($ array as $ value){{$ value}} @ endforeach';
$ blade = Blade :: compileString($ content);
$ php = eval($ blade);
</ code> </ pre>

这是我到目前为止的测试代码,它抛出异常</ p>

< p> ParseError:语法错误,意外的'&lt;',
期待文件结束</ p>
</ blockquote>

$ blade </ strong>的值 compileString()</ strong> </ p>

 &lt;?php $ __ currentLoopData = $ array;  $ __ env-&GT; addLoop($ __ currentLoopData);  foreach($ __ currentLoopData as $ value):$ __ env-&gt; incrementLoopIndices();  $ loop = $ __ env-&gt; getLastLoop();  ?&gt;&lt;?php echo e($ value);  ?&gt;&lt;?php endforeach;  $ __ env-&GT; popLoop();  $ loop = $ __ env-&gt; getLastLoop();  ?&gt; 
</ code> </ pre>

导致此错误的原因是什么? eval()与compileString()将刀片解析为php的方式不兼容吗?</ p>
</ div>



I'm trying to parse a blade template string into php format and use eval() to evaluate string as php code

 $array = [
     'foo' => 'bar',
     'bar' => 'foo'
 $content = '@foreach($array as $value){{$value}}@endforeach';
 $blade = Blade::compileString($content);
 $php = eval($blade);

This is my code for testing so far and it throws exception

ParseError: syntax error, unexpected '<', expecting end of file

value of $blade after compileString()

<?php $__currentLoopData = $array; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $value): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><?php echo e($value); ?><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

What causes this error? Is eval() not compatible with the way compileString() is parsing blade into php?


手册:</ p>

代码不能包含在打开和关闭的PHP标记中,即'echo“嗨! “;'</ code>必须传递而不是'&lt;?php echo”嗨!“; ?&GT;'</代码>。 尽管使用适当的PHP标签,例如,仍然可以离开并重新进入PHP模式。 'echo“在PHP模式下!”; ?&gt;在HTML模式下!&lt;?php echo“返回PHP模式!”;'</ code>。</ p>
</ blockquote>

您的刀片包装在&lt;?php </ code>标签,编译失败。 删除封闭的PHP标记,但保留插页式标记。</ p>
</ div>



As stated in the manual:

The code must not be wrapped in opening and closing PHP tags, i.e. 'echo "Hi!";' must be passed instead of '<?php echo "Hi!"; ?>'. It is still possible to leave and re-enter PHP mode though using the appropriate PHP tags, e.g. 'echo "In PHP mode!"; ?>In HTML mode!<?php echo "Back in PHP mode!";'.

Your blade is wrapped in <?php tags, and the compilation fails. Remove the enclosing PHP tags, but leave the interstitial tags.

Csdn user default icon