把一个php字符串放到html文件中,然后用file_get_contents回音?

问题描述 投票:0回答:1

我正在寻找一种方法将一个php字符串放入一个html文件中,我用file_get_contents拉入,然后回声。

PHP剪接。

if($_GET['title'] == "main"){
    $name = "Jan";
    $page = file_get_contents('pages/main.html');
    echo $page;
}

HTML Snipped:

<div id='personal_data'>
    Persönliche Daten:</br>
    Name: $name</br>
    Alter: 18</br>
    Wohnort: Keine Ahnung
</div>
php html string put
1个回答
0
投票

我想在这里也有类似的问题:例如,如果你是一个普通人,你会怎么做?PHP向file_get_contents()发送变量。但是file_get_contents只是返回一个字符串,所以str_replace应该可以用。

# https://www.php.net/manual/en/function.str-replace.php
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");

# your example
if($_GET['title'] == "main"){
    $name = "Jan";
    $page = file_get_contents('pages/main.html');
    echo str_replace('$name', $name, $page);
}

0
投票

有了 file_get_contents() 你把HTML文件当作纯文本文件来处理,PHP不会对它进行解析或解释。在 echo 就会打印出这个文件的原始内容。

如果你是PHP新手,你想加载一个页面并解释变量,我建议你先看看网上的一些PHP介绍专题& 教程。除非你要建立一种页面缓存,否则这对你来说,维护起来并不容易,也不实用。

顺便说一下,如果你想把一个文件中的HTML内容 "包含 "到你的主PHP文件中,你必须使用 include()require() 函数。

下面是一些文件收录的例子。https:/www.tutorialspoint.comphpphp_file_inclusion.htm

© www.soinside.com 2019 - 2024. All rights reserved.