dougou6727 2013-10-04 23:37
浏览 59

在外部PHP文件中回显ob_start回调


I'm working on population static HTML with database information in PHP. The HTML files have a pattern ({{$pattern}}) which when located in an HTML file will be replaced with a matching PHP variable. An example HTML file could be:

<div>{{$apples}}</div>

To populate this HTML file on demand, I've created the following PHP script (prepend.inc):

function ob_callback($buffer){

    global $data;
    $data['apples'] = CONSTANT; //global definition defined elsewhere

    $buffer = preg_replace_callback(
          '/{{\$([a-zA-Z0-9_]+)}}/',
          function($matches){
                 global $data;
             return (isset($data[$matches[1]])?$data[$matches[1]]:"");
          },
          $buffer);
    return $buffer;
}
ob_start("ob_callback");

This scrip is prepended to every HTML file by adding the following to the .htaccess in the HTML directory:

AddType application/x-httpd-php .html
php_value auto_prepend_file "prepend.inc"

Because of the architecture of my application, I require an HTML file echo'ed in PHP.

This has however proven to be a bigger task than expected because any attempt I make at converting the file to a PHP string and echo'ing results in the above ob_callback not being executed... (Althrough I know prepend.inc is run)

I have tried fread, file_get_content and even:

const CONSTANT = 'oranges';
ob_start();
require 'theHTMLfile.html';
$out = ob_get_contents();
ob_end_clean();
echo $out;

None of these however seem to have reached the ob_start callback in prepend.inc, and have had the pattern replaced...
One solution would be to use the full URL (require 'http://www.example.com/theHTMLfile.html';), but I CANNOT use that as it would break the reference to global variables, that I require to fetch data for the HTML etc..

Any suggestions would be much appreciated!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算