By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,367 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

How To Check For Banned Word On Page With Javascript ?

P: 39
Folks,

I'm trying to add a content filter (banned words filter) onto a web proxy. When the Javascript detects any of the banned words on the page, it should:

* As soon as a banned word is detected, it should not check for any more but immediately echo/print on screen/display a warning to the user with the first banned word spotted:
"Banned word "blah" found on page. You will be redirected to Google".
* Then, redirect to Google.
* Dump to database the banned word found, and the url of the page the banned word was found on. Eg.

Banned Word Found|Url|Date & Time of the Server
--------------------------------------------------------
ass|donkey-ass.com|25-01-2007, 03:00:00



The only requirement is that, I should be able to feed a list of banned words it should check for.

What would the code be to do all that ?
I'd like to code see samples.
I'm trying to build one in php but the php gurus say it is best the banned words checking is done on client-side. Else, the page would take too long to load (high cpu usage) if the checking is done on server side. I have no experience in writing Javascript code.


Thank You!
Oct 5 '17 #1
Share this Question
Share on Google+
2 Replies


Expert Mod 100+
P: 2,324
For live checking of words as they're being typed yes javascript might be the best approach.

You should use a library like jquery to consume the input and run it through a filter and set the alert when it's found.

Take a look at the following github rep
https://github.com/ChaseFlorell/jQuery.ProfanityFilter
Oct 5 '17 #2

P: 39
Folks!

Why do you reckon this code does not work ? I only see a blank page.
The code will need to run everytime cURL loads a page so that the JS can check the page for banned words.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /*
  3. ERROR HANDLING
  4. */
  5. // 1). Set banned words.
  6. $banned_words = array("Prick","Dick", "blow", "cock");
  7. // 2). $curl is going to be data type curl resource.
  8. $curl = curl_init();
  9. // 3). Set cURL options.
  10. curl_setopt($curl, CURLOPT_URL, 'https://www.buzzfeed.com/mjs538/the-68-words-you-cant-say-on-tv?utm_term=.xlN0R1Go89#.pbdl8dYm3X');
  11. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  12. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
  13. // 4). Run cURL (execute http request).
  14. $result = curl_exec($curl);
  15. if (curl_errno($curl)) {
  16.     echo 'Error:' . curl_error($curl);
  17. }
  18. $response = curl_getinfo( $curl );
  19. if($response['http_code'] == '200' )
  20. {
  21. ?>
  22. <script>
  23. $(function(){
  24.         var pageData = "hello <a>example can have tags! swear word is </a><div> one..</div>";
  25.    if(checkSwear(pageData)== false){
  26.    //redirect to google
  27.    }
  28. });
  29.  
  30. function checkSwear(sentance) {
  31.  
  32.     var swear_words_arr=new Array("blow", "nut", "asshole");
  33.   var regex = new RegExp('\\b(' + swear_words_arr.join('|') + ')\\b', 'i' );
  34.   if(regex.test(sentance)) {
  35.     alert("Please refrain from using offensive words"); /* + alert_text */
  36.     return false;
  37.   } else {
  38.   //alert(1)
  39.       return true;
  40.   }
  41. }
  42. </script>
  43. <?php
  44. }
  45. curl_close($curl);
  46. ?>
  47.  
  48.  
Oct 5 '17 #3

Post your reply

Sign in to post your reply or Sign up for a free account.