Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post return filtered results to page

    Hi there,

    I'm a total n00b when it comes to PHP, so you'll have to pardon my ignorance. I have a basic foundation in javascript/html/css/jquery so I'm not completely uninitiated… Essentially what I want to do is implement the following script:

    PHP Code:
    <?
     
    // BadWordFilter
    // This function does all the work. If $replace is 1 it will replace all bad words
    // with the wildcard replacements.  If $replace is 0 it will not replace anything.
    // In either case, it will return 1 if it found bad words or 0 otherwise.
    // Be sure to fill the $bads array with the bad words you want filtered.
     
    FUNCTION BadWordFilter(&$text$replace){
     
         
    // fill this array with the bad words you want to filter and their replacements
         
    $bads = ARRAY (
              ARRAY(
    "butt","b***"),
              ARRAY(
    "poop","p***"),
              ARRAY(
    "crap","c***")
         );
     
         IF(
    $replace==1) {                                        //we are replacing
              
    $remember $text;
     
              FOR(
    $i=0;$i<sizeof($bads);$i++) {               //go through each bad word
                   
    $text EREGI_REPLACE($bads[$i][0],$bads[$i][1],$text); //replace it
              
    }
     
              IF(
    $remember!=$text) RETURN 1;                     //if there are any changes, return 1
     
         
    } ELSE {                                                  //we are just checking
     
              
    FOR($i=0;$i<sizeof($bads);$i++) {               //go through each bad word
                   
    IF(EREGI($bads[$i][0],$text)) RETURN 1//if we find any, return 1
              
    }     
         }
    }
     
    // this will replace all bad words with their replacements. $any is 1 if it found any
    $any BadWordFilter($wordsToFilter,1); 
     
    // this will not repace any bad words. $any is 1 if it found any
    $any BadWordFilter($wordsToFilter,0); 
     
    ?>
    I will have a page with an html form, one text field and a submit button. I'm guessing that I need to save the above script to a PHP file, and set that file as the action for the submit button in the html form. The problem is - how do I return the filtered results to the next page?

    Any thoughts?

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    First, ereg is now deprecated... use preg.

    As for how to pass it to the next page, store it in a session and move it along.

    If this is your first/among your first PHP scripts, I recommend just taking a step back, make sure you properly understand what the function is doing. Besides that, feel free to ask how to do pieces.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The function itself doesn't return a filtered result. To pass it from a form, you simply include this function in a viewable scope (remove the two lines from the bottom as they may generate errors), and pass in your data, probably from a $_POST['txtInput'] of some sorts. Then use like so:
    PHP Code:
    if (badWordFilter($_POST['txtInput'], 1))
    {
        print 
    'bad words have been replaced.';
    }
    print 
    $_POST['txtInput']; 
    The function signature of FUNCTION BadWordFilter(&$text, $replace) states that the $text is run against a reference of the provided value. This is an indication that the $text is modified within the code itself, and no result needs to be returned (this returns 1 or 0 to indicate true or false for replacements, though it could return void).
    Keleth has already mentioned that ereg is deprecated. This is because PHP has followed the PCRE over the POSIX compliant replacements, so you should use preg instead. This function is however bloated; there is no reason to pattern match this at all since there is no binding occurring to specific words, just to letters (it will replace in the middle of words). This can be achieved much faster and easier using str_replace instead.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    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
    •