用一堆代码php str_replace
作者:互联网
我有一些来自数据库的内容.我想用一堆代码替换内容的特定单词.
来自数据库的内容例如:
Thank you for interest on our web site.
{FORMINSERT}
You can
also contact us by calling us to 1234567890
我想用一堆PHP代码替换字符串{FORMINSERT}.如果它是普通的文本字符串,我可以简单地使用str_replace替换它.
但是替换的内容不是简单的文本,而是表单代码.
要替换此{FORMINSERT}
举例:
<form method="post" id="form1" action="<?php echo KT_escapeAttribute(KT_getFullUri()); ?>">
<table cellpadding="5" cellspacing="2" >
<tr>
<td width="84" ><a name="contact" id="contact"></a></td>
<td width="384"> </td>
</tr>
<tr>
<td colspan="2" ><h1>Contact Us</h1></td>
</tr>
<tr>
<td ><label for="fullname">Name:</label></td>
<td>
<input type="text" name="fullname" id="fullname" value="<?php echo KT_escapeAttribute($row_rsscotts_contact['fullname']); ?>" size="47" />
<?php echo $tNGs->displayFieldHint("fullname");?> <?php echo $tNGs->displayFieldError("scotts_contact", "fullname"); ?>
</td>
</tr>
<tr>
<td ><label for="phone">Phone:</label></td>
<td>
<input type="text" name="phone" id="phone" value="<?php echo KT_escapeAttribute($row_rsscotts_contact['phone']); ?>" size="47" />
<?php echo $tNGs->displayFieldHint("phone");?> <?php echo $tNGs->displayFieldError("scotts_contact", "phone"); ?>
</td>
</tr>
<tr>
<td><label for="email">Email:</label></td>
<td>
<input type="text" name="email" id="email" value="<?php echo KT_escapeAttribute($row_rsscotts_contact['email']); ?>" size="47" />
<?php //echo $tNGs->displayFieldHint("email");?> <?php echo $tNGs->displayFieldError("scotts_contact", "email"); ?>
</td>
</tr>
<tr>
<td><label for="tellus">Looking for:</label></td>
<td>
<textarea name="tellus" id="tellus" cols="37" rows="5"><?php echo KT_escapeAttribute($row_rsscotts_contact['tellus']); ?></textarea>
<?php echo $tNGs->displayFieldHint("tellus");?> <?php echo $tNGs->displayFieldError("scotts_contact", "tellus"); ?>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="KT_Insert1" id="KT_Insert1" value="Submit" class="button-blue" />
<input name="Reset" type="reset" value="Reset" class="button-grey" />
</td>
</tr>
</table>
</form>
解决方法:
test1.php:
$database_content = 'Thank you for interest on our web site.
{FORMINSERT}
You can also contact us by calling us to 1234567890';
if(stripos($database_content, '{FORMINSERT}') !== FALSE){
ob_start();
include 'test2.php';
$result = ob_get_clean();
}
$database_content = str_replace("{FORMINSERT}", $result, $database_content);
echo $database_content;
test2.php(您要插入的代码):
echo 'hello world';
结果:
Thank you for interest on our web site. hello world You can also
contact us by calling us to 1234567890
就像代码“回声’hello world’;”一样正坐在{FORMINSERT}所在的位置.您可以只创建一堆PHP文件来包含类似内容,然后创建一些if语句来处理替换文件.
标签:str-replace,forms,replace,string,php 来源: https://codeday.me/bug/20191031/1976274.html