String Replace

Steph'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,

Steph

Text after replaced text

Thank you for registering for our conference. Please print out and keep this receipt.

Title: 2026 Conference on Technical Stuff

Cost: $ 249.95

The dates of the Conference are: Nov 18-25, 2026

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,

Steph
<?php
include ('../header/assignmentHeader.php');
error_reporting(E_ALL);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>String Replace</title>
    <link rel="stylesheet" href="../css/assignmentHeaderStyles.css" />
</head>
<body>

<h1>Steph'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

echo("<p>Original text</p>");

$text = '';
$fcontents = file('purchase_confirm.txt');

foreach (
$fcontents as $value) {
    
$text .= $value;
}

echo(
"<pre class='wide'>$text</pre>");

$title = "2026 Conference on Technical Stuff";
$cost = 249.95;
$dateinfo = "Nov 18-25, 2026";

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;
}

echo(
"<pre class='wide'>$text</pre>");

?>

</body>
</html>
<?php
show_source
(__FILE__);
include(
'../footer/footer.php');
?>