douqian1517 2015-08-26 22:46
浏览 33
已采纳

内部带有<?PHP?>标签的PHP create_function

I´m building a MVC style parser, and I NEEDto be able to process my custom tags (used with the {} separator, like {echo $myVar}) together with standard PHP code <?php doSomething ?> in the same file...

So, after running my parser (that deals with the {} scope), I get the following result:

if (is_Array($data) && sizeof($data)) extract($data); 
$text = array();$text[] = "
<div class=\"row\">

            <strong>Name: </strong>";$text[] =  $name;$text[] = "<br /> 
            <strong>Description:</strong>";$text[] =  $description;$text[] = "
            <br /> 

            <strong>Status: </strong>
            <?php if ($status == false) : ?>
                <span class=\"label label-danger\">FAILURE</span>
            <?php else : ?>
                <span class=\"label label-success\">OK</span>
            <?php endif; ?> 
            <br />
    </div>
";return implode($text);

All fine. From that piece of code I create a function using:

function = create_function("\$data", $code); 

$code is the resultset from above. $data is an array containg variables and values like:

`$name` => jonas
`$description` => This is test.
`$status`=> false

Finally I run:

$ret = $function($data);
echo $ret;

At that point, I get a problem. All references to variables are removed, but the tags are not processed, resulting in a kind of wierd code like:

<div class="row">

            <strong>Name: </strong>jonas"<br /> 
            <strong>Description:</strong>This is test.<br /> 

            <strong>Status: </strong>
            <?php if ( == false) : ?>  <<<<<========== WRONG CODE
                <span class=\"label label-danger\">FAILURE</span>
            <?php else : ?>
                <span class=\"label label-success\">OK</span>
            <?php endif; ?> 
            <br />
    </div>

So, the variables are substituted, except for the code that is neither solved nor have the variables correctly replaced.

I wish I can get a solution for that problem... Thanks for helping.

  • 写回答

2条回答 默认 最新

  • dongse5528 2015-08-26 22:51
    关注

    You're probably going about this in an inefficient manner...but to purely fix the problem you are having with your current code, just take the conditionals out of the quotations.

    if (is_Array($data) && sizeof($data)) extract($data); 
    $text = array();$text[] = "
    <div class=\"row\">
    
                <strong>Name: </strong>";$text[] =  $name;$text[] = "<br /> 
                <strong>Description:</strong>";$text[] =  $description;$text[] = "
                <br /> 
    
                <strong>Status: </strong>";
                if ($status == false) :
                    $text[] = "<span class=\"label label-danger\">FAILURE</span>";
                else :
                    $text[] = "<span class=\"label label-success\">OK</span>";
                endif;
                $text[] = "<br />
        </div>
    ";return implode($text);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助