处理PHP的文件的内容\u get\u content()
我需要在一个文件(子文件)上使用一些PHP标记,该文件(子文件)正在使用file_get_contents(子文件)调用到模板中 我最初使用require(子文件)而不是file_get_contents()。但由于我的内容放错了位置,所以它不太管用。而且我无法将模板文件更改为使用require() 我的模板如下所示:处理PHP的文件的内容\u get\u content(),php,file-get-contents,Php,File Get Contents,我需要在一个文件(子文件)上使用一些PHP标记,该文件(子文件)正在使用file_get_contents(子文件)调用到模板中 我最初使用require(子文件)而不是file_get_contents()。但由于我的内容放错了位置,所以它不太管用。而且我无法将模板文件更改为使用require() 我的模板如下所示: //TEMPLATE FILE.php $html = file_get_contents(subFile); //I CAN NOT CHANGE THIS FILE 然后
//TEMPLATE FILE.php
$html = file_get_contents(subFile); //I CAN NOT CHANGE THIS FILE
然后是我的子文件,我可以更改的子文件
//SUB FILE.php
<div>Hello world, today is <?php echo date($mydate);?> </div>
因此,考虑到我无法更改使用文件\u get\u content()的模板文件.php
。
我在想,是否有一种方法可以包装SUB FILE.php的内容,以便它在允许模板文件.php调用php之前对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();
以下是官方手册的一个示例
$homepage = file_get_contents('http://www.example.com');
echo $homepage;
试试这个:
file_get_contents('http://YourDomain/Yoursubfile');
您不能使用
来回显变量$a的内容。
因为您将从子文件中获取普通html内容
您可以做的是:-
步骤1:my_date.php
<?php
$today = date('M-d-Y');
$subfile_html = file_get_contents(subFile); //Path to my_date1.html or my_date1.php
echo $new_html_content = str_replace('{TODAYS_DATE}', $today, $subfile_html); // It will replace the occurance {TODAYS_DATE} with today's date
// OUTPUT:
// Hello world, today is June 20 2014
?>
您可以通过web服务器访问子文件吗?然后您可以执行文件获取内容(“http://localhost/subFile.php");
和PHP将被执行。问题是我无法更改发生文件\u get\u contents()的模板文件:(那么我认为你运气不好。你不能通过更改子文件来解决这个问题,它必须在使用它的脚本中完成。模板脚本只是读取文件,而读取文件并不执行它。file\u获取内容
和require
执行两个完全不同的功能。你实际上是在读取文件的数据在第一个示例中,而第二个示例将传递的文件内容的内容添加为包含它的PHP。
$homepage = file_get_contents('http://www.example.com');
echo $homepage;
file_get_contents('http://YourDomain/Yoursubfile');
<?php
$today = date('M-d-Y');
$subfile_html = file_get_contents(subFile); //Path to my_date1.html or my_date1.php
echo $new_html_content = str_replace('{TODAYS_DATE}', $today, $subfile_html); // It will replace the occurance {TODAYS_DATE} with today's date
// OUTPUT:
// Hello world, today is June 20 2014
?>
<div> Hello world, today is {TODAYS_DATE} </div>