www.webdeveloper.com
Results 1 to 2 of 2

Thread: Preg Match logic flaws

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    1

    Preg Match logic flaws

    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!!

    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 />";
    // } 

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    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

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles