India4Design India4Design - 3 months ago 8
PHP Question

str_replace with a bunch of code php

I have some content coming from database. And I want to replace the specific word of the content with a bunch of code.

Content coming from database is for example:


Thank you for interest on our web site.

{FORMINSERT}

You can
also contact us by calling us to 1234567890


I want to replace the string
{FORMINSERT}
with a bunch of PHP code. If it is a normal text string I can simply replace it by using
str_replace
.

But the replacing content is not simple text it is form code.

want to replace this
{FORMINSERT}


with example:

<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">&nbsp;</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>

Answer

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 (the code you're trying to insert in):

echo 'hello world';

The result:

Thank you for interest on our web site. hello world You can also contact us by calling us to 1234567890

So it is as if the code "echo 'hello world';" was sitting right where {FORMINSERT} was. You can just create a bunch of PHP files to include like that, and make some if statements to handle the replacements.