Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Preg match logic

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Preg match logic

Postby AsapFerg » Fri Oct 05, 2012 12:43 pm

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. How do I run through my array with preg replace all and swap the users input with my patterns in 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. Thank you!!
Code: Select all
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 />";
// }
AsapFerg
New php-forum User
New php-forum User
 
Posts: 1
Joined: Fri Oct 05, 2012 12:39 pm

Re: Preg match logic

Postby johnj » Sat Oct 06, 2012 6:29 am

johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm


Return to PHP coding => General

Who is online

Users browsing this forum: ScroogeMcDuck and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.