
October 5th, 2012, 03:36 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 1
Time spent in forums: 6 m 48 sec
Reputation Power: 0
|
|
|
Preg Match logic flaw
Hello  everyone. I'm working on a madlib program for my web programming class but I'm very lost in replacing users input with my story. Here is my code so far, the last function is where it goes blank. I want the users to enter words and they get substitued with the patterns in my story.
PHP Code:
require "classfun.php";
printDocHeading("./index.css", "assignment 4");
if(empty($_POST))
{
Storyselection();
}
else if($_POST)
{
Generateform();
}
else if($hide)
{
Displaystories();
}
printDocFooter();
function Storyselection()
{
$self = $_SERVER['PHP_SELF'];
print "<body>";
print "<div class='content'>\n";
print "<p> Pick a story! </p>";
print "<form method='post' action='$self' >\n";
print "<p> <input type='radio' name='story' value='1' /> \nFunny Story ".
"<input type='radio' name='story' value='2' /> \nSpooky Story ".
"<input type='radio' name='story' value='3' /> \nRegular Story ".
"</p>\n".
"<p> <input type='submit' name='submitStory' value='submitchoice'>\n".
"</p>\n".
"</form>\n".
"</div>\n";
}
function Generateform()
{
//print "<div class ='stor'>\n".
// "<h3> Enter the words into the forms </h3>\n".
print "<form method ='post' action= '$_SERVER[PHP_SELF]'>\n";
$story = htmlentities($_POST['story']);
$filename2 ="story"."$story"."-"."$story".".txt";
$fh2 = file_get_contents($filename2);
// print nl2br($fh2);
// print "<p> here are the matches </p>";
$pattern = "/\[(.+?)-(.+?)\]/";
preg_match_all($pattern,$fh2,$arrayOfMatches);
for($i=0; $i<count($arrayOfMatches[0]);$i++)
{
// print $arrayOfMatches[0][$i]."<br/>";
$desc = $arrayOfMatches[2][$i];
$name = $arrayOfMatches[1][$i];
print " <p>$desc <input type='text' name ='$name' value = 'sub' />\n"."</p>";
}
print"<input type ='hidden' name ='story' value= '$hide' /> \n";
print"<input type='submit' name='submitStory' value='submitsubs'>\n".
"</form>";
//print "</div>\n";
// print "<a href='".$_SERVER['PHP_SELF']."'>Back to Start</a>\n";
}
// for($i=0;$i<count($matches[0]);$i++)
// {$desc = $matches[2][$i];
// $name = $matches[1][$i];
// <form method ='post' action= '$_SERVER[PHP_SELF]'>;
// <input ='text' name = '$name' value=' />;
// <input type ='submit' name='submitsubs' value='submitstor'>";
// <"input type = 'hidden' name = 'story' value= '$hide' /> \n";
// // generate form and send hidden value
//
// loop to substiute entered data with patterns
// $subject=preg_replace($pattern,$replacement,$subject)
// $name = $matches[1][$i];
// $pattern= "/\[$name-(.+?)\]/";
// $replace =$_POST[$name];
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function Displaystories()
{
//display origianl story
print "<div class ='stor'>\n".
"<h3> Here is the original Story </h3>\n".
"</div>\n";
$story = htmlentities($_POST['$hide']);
$filename2 ="story"."$hide"."-"."$hide".".txt";
$fh2 = file_get_contents($filename2);
$subject=preg_replace($pattern,$fh2,$arrayofMatches);
for($i=0; $i<count($arrayOfMatches[0]);$i++)
{
// print $arrayOfMatches[0][$i]."<br/>";
$desc = $arrayOfMatches[2][$i];
$name = $arrayOfMatches[1][$i];
$pattern= "/\[$name-(.+?)\]/";
$replace =$_POST[$name];
}
print nl2br($fh2);
// display sub story
// return to start menu button
//print "<a href='".$_SERVER['PHP_SELF']."'>Back to Start</a>\n";
//
//
}
//
//foreach($matches[0] as $match) {
// print " $match <br />";
// }
// print "<h4>Here is the 1st group: </h4>";
// foreach($matches[1] as $match) {
// print " $match <br />";
// }
// print "<h4>Here is the second group: </h4>";
// foreach($matches[2] as $match) {
// print " $match <br />";
// }
[[ please use [php][/php] tags when posting PHP code --requinix ]]
Last edited by requinix : October 5th, 2012 at 03:46 PM.
|