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!!
}
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 />";
// }
10-05-2012, 07:53 PM
NogDog
One problem I see is that you have variables in your functions which will be local in scope only to those functions, but it appears you are expecting to be globally scoped (accessible anywhere). For instance, in the Displaystories() function you reference the $pattern variable, but it is not defined anywhere in that function. While it is defined in an earlier function, it will only be "known" by that earlier function. (The exception to this is the "super-global" arrays such as $_POST, which are "known" everywhere within a PHP script). The typical solution is to pass such variables into the function as a function parameter, e.g.:
PHP Code:
function Displaystories($pattern) { // do something with $pattern }
// call the function: $pattern = '/foo*/'; Displaystories($pattern);
// it doesn't even have to be the same name: $myRegExpToBeUsed = '/^\s?\d+$/'; Displaystories($myRegExpToBeUsed);
PS: Proper and consistent indenting of your code can make it easier for you and us to debug. (Any decent code editor can make indenting easier.) Also, you might want to add these lines during development to make sure you're seeing everything that PHP has to complain about (though you likely wouldn't want it in your production version):
PHP Code:
<?php ini_set('display_errors', true); error_reporting(E_ALL); // rest of code... ?>