php - 将 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>
解决方案
我认为这里有类似的问题:例如。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);
}
推荐阅读
- python-3.x - Python:在 concurrent.futures.ProcessPoolExecutor() 的 executor.map 中获取多个返回值并提供多个参数
- java - HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found
- c# - 派生类的 C# 验证属性
- reactjs - TypeError:无法读取未定义的“内容”属性
- amazon-web-services - 使用 DynamoDB 和 Amazon API Gateway 的验证异常
- java - 使用来自 android studio 应用程序的输入数据生成 3d 模型
- composer-php - 简单的作曲家缓存因 bitbucket 管道而失败
- javascript - 转换为画布时如何打开详细信息标签
- c - main.c 无法读取我在 .h 文件中声明并在另一个 .c 文件中定义的函数(在 C 中)
- amazon-web-services - 如何在 cloudformation 堆栈中获取现有的 VPC cidr