在PHP中如何使用用户定义的变量包含文件

huangapple go评论94阅读模式
英文:

How to include file in PHP with user defined variable

问题

我试图在字符串替换中包含文件,但在输出中得到的是字符串而不是最终输出。

analytic.php

  1. <?php echo "<title> Hello world </title>"; ?>

head.php

  1. <?php include "analytic.php"; ?>

index.php

  1. string = " <head> </head>";
  2. $headin = file_get_contents('head.php');
  3. $head = str_replace('"<head>"', '"<head>"'. $headin, $head);
  4. echo $head;

我得到的输出 :

  1. <head><?php include "analytic.php"; ?> </head>

我需要的输出 :

  1. <head><title> Hello world </title> </head>

注意 : 请不要建议在index.php中直接使用analytic.php,因为head.php中包含一些重要的代码,必须将analytic.phphead.php合并,然后在index.php中使用。

英文:

I am trying to include file in string replace but in output i am getting string not the final output.

analytic.php

  1. <?php echo "<title> Hello world </title>"; ?>

head.php

  1. <?php include "analytic.php"; ?>

index.php

  1. string = " <head> </head>";
  2. $headin = file_get_contents('head.php');
  3. $head = str_replace("<head>", "<head>". $headin, $head);
  4. echo $head;

Output i am getting :

  1. <head><?php include "analytic.php"; ?> </head>

Output i need :

  1. <head><title> Hello world </title> </head>

Note : Please do not recommend using analytic.php directly in index.php because head.php have some important code and it has to be merged analytic.php with head.php and then index.php

答案1

得分: 2

要获得所需的输出:

  1. function getEvaluatedContent($include_files) {
  2. $content = file_get_contents($include_files);
  3. ob_start();
  4. eval("?>$content");
  5. $evaluatedContent = ob_get_contents();
  6. ob_end_clean();
  7. return $evaluatedContent;
  8. }
  9. $headin = getEvaluatedContent('head.php');
  10. $string = "<head> </head>";
  11. $head = str_replace("<head>", "<head>" . $headin, $head);
  12. echo $head;

输出将是output string而不是file string

  1. <head><title> Hello world </title> </head>
英文:

To get the desired output :

  1. function getEvaluatedContent($include_files) {
  2. $content = file_get_contents($include_files);
  3. ob_start();
  4. eval(&quot;?&gt;$content&quot;);
  5. $evaluatedContent = ob_get_contents();
  6. ob_end_clean();
  7. return $evaluatedContent;
  8. }
  9. $headin = getEvaluatedContent(&#39;head.php&#39;);
  10. string = &quot; &lt;head&gt; &lt;/head&gt;&quot;;
  11. $head = str_replace(&quot;&lt;head&gt;&quot;, &quot;&lt;head&gt;&quot;. $headin, $head);
  12. echo $head;

Output will be output string not file string :

  1. &lt;head&gt;&lt;title&gt; Hello world &lt;/title&gt; &lt;/head&gt;

答案2

得分: 1

$file = file('绝对/路径/到/文件.php');

foreach ($file as $line => $code) {

  1. if (str_contains($code, '<head>')) {
  2. $file[$line] = str_replace('<head>', '<head>' . $headin, $code);
  3. break;
  4. }

}

file_put_contents('绝对/路径/到/文件.php', $file);

英文:

I think your approach is pretty basic (you try to hardcore modify - programmerly edit - the template script, right?) but anyway:

  1. $file = file(&#39;absolut/path/to/file.php&#39;);
  2. foreach ($file as $line =&gt; $code) {
  3. if (str_contains($code, &#39;&lt;head&gt;&#39;)) {
  4. $file[$line] = str_replace(&#39;&lt;head&gt;&#39;, &#39;&lt;head&gt;&#39; . $headin, $code);
  5. break;
  6. }
  7. }
  8. file_put_contents(&#39;absolut/path/to/file.php&#39;, $file);

huangapple
  • 本文由 发表于 2023年2月6日 17:39:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定