开发者

PHP to text function

I am trying to create a function that would parse php code and return the result in pure text, as if it was being read in a browser. Like this one:

public function PHPToText($data, $php_text) {
    //TODO code
    return $text; 
}

I would call the function like this, with the params that you see below:

$da开发者_StackOverflow社区ta = array('email' => 'test@so.com');
$string = "<?= " . '$data' . "['email']" . "?>";

$text = $this->PHPToText($data, $string);

Now echo $text should give: test@so.com

Any ideas or a function that can achieve this nicely?

Thanks!


It's a bad bad bad bad bad idea, but basically:

function PHPToText($data, $string) {
    ob_start();
    eval($string);
    return ob_get_clean();
}

You really should reconsider this sort of design. Executing dynamically generated code is essentially NEVER a good idea.


in this case it should be done with eval()

But always remember: eval is evil!


You will need to use the eval() function http://www.php.net/eval in order to parse the tags inside your variable $string

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜