User:Dave Young/Computata Miscellanea: Review Gen

From XPUB & Lens-Based wiki

Review Generator v0.01

Built in Nicolas Maleve's thematic project workshop. More information available here Below is the php code that shuffles the lines of the textfile and prints them to the webpage.

<?php

$goodlines = file('good.txt'); //the 'good' text file to read from
shuffle($goodlines); //randomise array order
$badlines = file('bad.txt');
shuffle($badlines);
$numLines = rand(1, 8); //random number of sentences in review - min 1, max 8 sentences

for ($i=0; $i<$numLines; $i++){

        $currline = $goodlines[$i]; //store the current line  $currline
        if(preg_match("*name*", $currline)){ //if *name* is found, insert the form element 'name'   
                $currline = preg_replace("*name*", htmlspecialchars($_POST['name']), $currline);
        }
        if(preg_match("*city*", $currline)){ //if *city* is found, insert form element 'city'
                $currline = preg_replace("*city*", htmlspecialchars($_POST['city']), $currline);
        }
        echo $currline; //print edited $currline
}
?>