如何从file_get_contents或函数中的文件运行php代码 [英] How to run php code from file_get_contents or file in a function

查看:159
本文介绍了如何从file_get_contents或函数中的文件运行php代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计自己的MVC模式来简化创建主页的过程。我的模板系统需要我的控制器类输出我的意见。这意味着我必须通过php函数输出文件。我一直在寻找一段时间,似乎无法找到解决办法。



如何通过PHP函数运行一个代表某些源代码的字符串(<?,<?php,?>和等等)作为PHP? Eval不会把我的< ?标志(我读这个函数是由于某种原因废话)。

解决方案

您可以执行PHP代码,并收集输出这个:

  ob_start(); 
包含template.phtml;
$ out1 = ob_get_clean();

http://www.php.net/manual/de/function.ob-get-contents.php


I am designing my own MVC pattern to ease the process of creating homepages. My templating system needs my controller class to output my views. This means I have to output the file through a php function. I have been searching for some a while now and can't seem to find a solution.

How can I, through a PHP function, run a string representing some source code ("< ?", "< ?php", "? >" and so on) as php? Eval would not take my < ? signs (and I read that function is crap for some reason).

解决方案

You could execute the php code and collect the output like this:

ob_start();
include "template.phtml";
$out1 = ob_get_clean();

http://www.php.net/manual/de/function.ob-get-contents.php

这篇关于如何从file_get_contents或函数中的文件运行php代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆