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!!
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... ?>
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks