首页 > 解决方案 > 将 php 字符串放入 html 文件中,同时使用 file_get_contents 回显?

问题描述

我正在寻找一种将 php 字符串放入 html 文件的方法,然后我使用 file_get_contents 将其拉入并回显。

PHP 截图:

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

HTML 截图:

<div id='personal_data'>
    Persönliche Daten:</br>
    Name: $name</br>
    Alter: 18</br>
    Wohnort: Keine Ahnung
</div>

标签: phphtmlstringput

解决方案


我认为这里有类似的问题:例如。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);
}

推荐阅读