使用PHP替换HTML占位符并生成HTML模板

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

我有一个 HTML 信件模板,其中包含字段值占位符,例如姓名、日期、租金、房东等。

 `<h6 class="date"><b>Date: <u>[DATE]</u></b></h6>`
 `<h6 class="tenant-name"><b>Tenant’s Name:<u>[TENANT_NAME]</u></b></h6>`
 `<h6 class="tenant-address"><b>Tenant’s Address:<u>[TENANT_ADDRESS]</u></b></h6>`
 `<h6 class="unit-number"><b>Tenant’s Apartment/Unit#: <u>[UNIT_NUMBER]</u></b></h6>`
 `<h6 class="city"><b>City:<u>[CITY]</u></b></h6>`
 `<h6 class="state"><b>State:<u>[STATE]</u></b></h6>`
 `<h6 class="zipcode"><b>Zip:<u>[ZIPCODE]</u></b> </h6>`

现在,我想使用 PHP 使用 file_get_contents 获取该文件,然后在文件上使用 str_replace 来查找这些短代码并将其替换为实际值。 最后,PHP 将呈现该内容。

这是我到目前为止所尝试过的。

`//create a variable to point to the filename`
`$file_name = 'landlord_letter.html";`

`//get data from letter template`
`$file_content = file_get_contents($file_name);`

`//replace place holders in letter template`
`$file_content = str_replace("[]", $class, $file_content)`
`$file_get_contents(str_replace$file_name, bool $use_include_path = false, resource $context,`               `int $start = -1, int $limit);`

我不确定这是否正确。如果是的话,我该去哪里?

php replace file-get-contents
© www.soinside.com 2019 - 2024. All rights reserved.