file_get_content()的内容以处理PHP [英] Contents of a file_get_content() to process PHP
问题描述
我需要在使用file_get_contents(Subfile)调用到模板的文件(子文件)上使用一些PHP标记.
I am need to use some PHP tags on a file(subfile) that is being called into a template using file_get_contents(Subfile).
我最初使用require(subFile)而不是file_get_contents().但是,由于我的内容放置在错误的位置,因此无法正常工作.而且我也不能更改模板文件以使用require().
I initially used require(subFile) instead of file_get_contents(). But it doesnt quite work as my content gets placed in the wrong spot. And also I can't change the template file to use require().
我的模板是这样的:
//TEMPLATE FILE.php
$html = file_get_contents(subFile); //I CAN NOT CHANGE THIS FILE
然后是我的子文件,我可以更改它.
Then my sub file, the one I can change.
//SUB FILE.php
<div>Hello world, today is <?php echo date($mydate);?> </div>
因为它是通过file_get_contents()调用的,所以放在这里
Because it is being called with file_get_contents(), it out puts
Hello world, today is <?php echo date($mydate);?>
代替:
Hello world, today is Thursday.
因此,考虑到我无法更改使用file_get_content()的TEMPLATE FILE.php
.
我在想是否可以包装SUB FILE.php的内容,以便在允许TEMPLATE FILE.php
调用它之前先处理PHP.
So, considering I can't change my TEMPLATE FILE.php
where the file_get_content() is used.
I was thinking if there is a way that I can wrap the content of SUB FILE.php so it would process the PHP before allowing it to be called by the TEMPLATE FILE.php
.
我想要的东西
//SUB FILE.php
<force Process the PHP ?>
<div>Hello world, today is <?php echo date($mydate);?> </div>
<Finishe Force Process ?>
<allow it to be called via file_get_content();
如果可以更改模板文件,则可以使用htmlspecialchars_decode();.但是我不能.
If I could change the template file I could use htmlspecialchars_decode(); but I can't.
不幸的是,我找不到解决办法,所以我最终还是找到了
Unfortunately I couldn't find a way to do that so I end up with this
ob_start();
$output = include (Dir/SubFile);
$output = ob_get_contents();
ob_end_clean();
$html = $output;
//$html = file_get_contents(subFile); The way it was
推荐答案
以下是官方手册的示例
$homepage = file_get_contents('http://www.example.com');
echo $homepage;
尝试一下:
file_get_contents('http://YourDomain/Yoursubfile');
这篇关于file_get_content()的内容以处理PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!