Veronica's String-Replace Demo with Form

This shows how one can find tokens within a text file and replace those tokens with php variables.
You can choose a conference name, cost, and dates in the form below, or see the default 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 
challenges to be overcome.

Sincerely,

Veronica 

Text after replaced text for Title, Cost, and Dates

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

Title: 2024 Conference on Technical Stuff

Cost: $ 249.36

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

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 
challenges to be overcome.

Sincerely,

Veronica 







 

CODE FOLLOWS

<?php
include('includes/header.php');

// set error reporting to all
error_reporting(E_ALL);

// variables
$self basename($_SERVER['SCRIPT_NAME']);
$name = isset($_POST['name']) ? $_POST['name'] : '';
$cost = isset($_POST['cost']) ? $_POST['cost'] : '';
$dates = isset($_POST['dates']) ? $_POST['dates'] : '';

// filter all input for browser output
$name htmlentities($name);
$cost htmlentities($cost);
$dates htmlentities($dates);

?>

<h1>Veronica's String-Replace Demo with Form</h1>

<p>
    This shows how one can find tokens within a text file and
    replace those tokens with php variables.<br> You can choose a
    conference name, cost, and dates in the form below, or see the default
    variables.
</p>

<?php

// get the file
$myFile '../docs/purchase_confirm.txt';

$text '';
$newText '';

// update with defaults if empty strings
if (strlen($name) < 1)
{
    
$name "2024 Conference on Technical Stuff";
}
if (
strlen($cost) < 1)
{
    
$cost "249.36";
}
if (
strlen($dates) < 1)
{
    
$dates "Nov 18-25, 2024";
}

// replace the original text with the stuff we want
if (($fcontents file($myFile)))
{
    
// here's the original text
    
echo("<p> Original text </p>");

    
// $fcontents is an array so append the contents of the array; could use implode
    
foreach ($fcontents as $value)
    {
        
// append contents to $text
        
$text .= $value;
        
// replace
        
$value str_replace("[TITLE]"$name$value);
        
$value str_replace("[COST]"$cost$value);
        
$value str_replace("[DATES]"$dates$value);
        
// new text value
        
$newText .= $value;
    }
    echo(
"<pre class='wide'> $text </pre>");

    
// replaced the original with new text
    
echo("<br><p>Text after replaced text for Title, Cost, and Dates </p>");

    echo(
"<pre class='wide'> $newText </pre>");
}
else
{
    echo(
"<br><p>file error </p>");
}

?>

<br>
<form action="<?php echo($self);?>" method="POST" >
    <label for="name">Conference Name</label>
    <input type="text" size="36" id="name" name="name" value="">
    <br><br>
    <label for="cost">Cost</label>
    <input type="text" size="36" id="cost" name="cost" value="">
    <br><br>
    <label for="dates">Dates</label>
    <input type="text" size="36" id="dates" name="dates" value="">
    <br><br>
    <input type="submit" name="submit" value="Submit">
</form>

<?php

include('includes/footer.php');
?>