filter some words while submitting form

Please support our PHP advertiser: 50% off 6 Months Dedicated Server Hosting from 1&1!
Reply

Join Date: Sep 2008
Posts: 159
Reputation: rajeesh_rsn is an unknown quantity at this point 
Solved Threads: 0
rajeesh_rsn rajeesh_rsn is offline Offline
Junior Poster

filter some words while submitting form

 
0
  #1
Nov 10th, 2009
Hai,
I had developed a self forum ( not third party ) for my web site. Working fine... But now I need to filter some words while submitting the form. Ie If I need to filter the word "duck" in forum then if an user type "duck" in any of the text box/area then I need to change that into "d**k" on form submitting.

Please give me a little advice to solve this problem...

Thanks in advance
Rajeesh
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 512
Reputation: Atli is on a distinguished road 
Solved Threads: 63
Atli's Avatar
Atli Atli is offline Offline
Posting Pro
 
0
  #2
Nov 10th, 2009
Hey.

Consider this:
  1. <?php
  2. /**
  3.  * Obfuscates a word by replacing all but the first and the last letters with *
  4.  * @param string $word The unscrabled word.
  5.  * @return string
  6.  */
  7. function obfuscateWord($word)
  8. {
  9. $first = $word[0];
  10. $last = $word[strlen($word)-1];
  11. $middle = "";
  12. for($i = 0; $i < strlen($word)-2; $i++)
  13. {
  14. $middle .= "*";
  15. }
  16. return $first . $middle . $last;
  17. }
  18.  
  19. // A list of banned words
  20. $wordList = array('duck', 'fick');
  21.  
  22. // Compile the regular expression that searches for the words in the list.
  23. $regexp = '/(' . implode('|', $wordList) . ')/ie';
  24.  
  25. // The "callback" for the regular exrepssion matches.
  26. // This calls the obfuscateWord function, passing the matches word and replacing it
  27. // with the scrambled word.
  28. $replace = 'obfuscateWord("$1");';
  29.  
  30. // Example text to use
  31. $text = "There is a duck on the fick!";
  32.  
  33. // Do the actual replace.
  34. $text = preg_replace($regexp, $replace, $text);
  35.  
  36. echo $text;
  37. ?>
That should work, right?
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 159
Reputation: rajeesh_rsn is an unknown quantity at this point 
Solved Threads: 0
rajeesh_rsn rajeesh_rsn is offline Offline
Junior Poster

Thanks for your kind help

 
0
  #3
Nov 10th, 2009
Thanks for your kind help... let me check ...

Once again thanks a lot

Regards
Rajeesh
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 258 | Replies: 2
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC