How to include a php file in a html file via a php

2019-09-22 06:50发布

This question is an exact duplicate of:

I already post this question before but people always ask unnecessary question. I'm gonna explain it in a simple way.

I HAVE 3 files :

  1. a php file (contains only html, thats important) : We call it X file for the example.
  2. a php file where there's some database query to insert database data on the screen : Y file
  3. a php file (a script that will make some manipulations) : Z file

SO, i want to include Y into X with the script of Z.

In Z, i make a str_replace($text, $new, file_get_contents($file));

The ONLY THING is that i need to include PHP open and close TAGS in X because there's no php tags in it.

So, $new = "<?php include('Y.php'); ?>";. If you try, the close tag wont be considered in the string, but that's what i want.

Hope this question is NOW clear. I can't be more clearer than that. :D

Thanks for you advice.

2条回答
该账号已被封号
2楼-- · 2019-09-22 07:22

The question is very unclear and vague, but I have a guess:

File X is some HTML where you want to replace special markers.
File Y loads the value from DB that should replace the marker.
File Z does the replacement.

This could be solved like that (File Z):

<?php
ob_start();
include("Y.php");
$repl = ob_get_contents();
ob_end_clean();

ob_start();
include("X.php");
$src = ob_get_contents();
ob_end_clean();

echo str_replace("THEREPLACEMENTMARKER", $repl, $src);
?>
查看更多
戒情不戒烟
3楼-- · 2019-09-22 07:37

To include a file you do

<?php
include(file);
?>

So in this case

X contains <?php include('Y'); ?> And Y contains <?php include('Z') ?>

But if you are doing what i think you are doing (a template of some sort) you would be better of by looking into overflow buffer ( ob_start and ob_end_flush for example )

Those can place all echoed information into a variable to be modified later, also the php inside is run, instead of just read as text as in your example with the file_get_contents()

查看更多
登录 后发表回答