Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-22-2010, 06:44 PM   PM User | #1
bfinoradin
New to the CF scene

 
Join Date: Sep 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
bfinoradin is an unknown quantity at this point
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?
bfinoradin is offline   Reply With Quote
Old 09-22-2010, 06:58 PM   PM User | #2
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
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.
Keleth is offline   Reply With Quote
Old 09-22-2010, 07:24 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
filter, form, results, return

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:13 PM.


Advertisement
Log in to turn off these ads.