Tedd's String Replace Demo
This shows how one can find tokens within a text file and
replace those tokens with php variables.
Original text
Thank you for registering for our conference.
Please print out and keep this receipt.
Title: [TITLE]
Cost: $ [COST]
The dates of the Conference are: [DATES]
Please also pay special attention to the below instructions for how to
access your conference and be sure to attend the orientation during
the week before it begins.
Please let us know in the orientation/support forum if there are
problems. We want your experience to be educational and for the
technology to fade into the background.
Sincerely,
Tedd
Text after replaced text
Thank you for registering for our conference.
Please print out and keep this receipt.
Title: 2022 Conference on Technical Stuff
Cost: $ 249.95
The dates of the Conference are: Nov 18-25, 2022
Please also pay special attention to the below instructions for how to
access your conference and be sure to attend the orientation during
the week before it begins.
Please let us know in the orientation/support forum if there are
problems. We want your experience to be educational and for the
technology to fade into the background.
Sincerely,
Tedd
CODE FOLLOWS
<?php
include('includes/header.php');
error_reporting(E_ALL); // set error reporting to all
?>
<h1>Tedd's String Replace Demo</h1>
<p>
This shows how one can find tokens within a text file and
replace those tokens with php variables.
</p>
<?php
// here's the original text
echo("<p> Original text </p>");
$text = '';
$fcontents = file('purchase_confirm.txt'); // get the file
$text = '';
// $fcontents is an array so append the ontents of the array.
// certainly, I could use implode
foreach ($fcontents as $value)
{
$text .= $value; // append contents to $text
}
echo("<pre class='wide'> $text </pre>");
// now replace the original [text] with the stuff we want below
$fcontents = file('purchase_confirm.txt'); // load the original text
$title = "2022 Conference on Technical Stuff";
$cost = 249.95;
$dateinfo = "Nov 18-25, 2022";
$text = '';
echo("<br><p>Text after replaced text </p>");
$text = '';
foreach ($fcontents as $value)
{
$value = str_replace("[TITLE]", $title, $value);
$value = str_replace("[COST]", $cost, $value);
$value = str_replace("[DATES]", $dateinfo, $value);
$text .= $value; // append above to $text
}
echo("<pre class='wide'> $text </pre>");
include('includes/footer.php');
?>