doutao1171 2018-04-27 02:01
浏览 233
已采纳

Laravel - 带有刀片compileString的php eval

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?

  • 写回答

1条回答

  • dsxd62219570 2018-04-27 02:21
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?