CodeIgniter Forums

Full Version: Display a set_message for each bad wor found in string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
When the user types in a couple of word lets say ban and bad in string.

How could I get my set_message() to out put error message for each word found.

Currently only displays one message.


PHP Code:
<?php

class MY_Form_validation extends CI_Form_validation {

    public function 
badword($string) {
        
// Dummy words
        
$badWords = array("ban","bad","user","pass","stack","name","html");
        
$matches = array();
        
$matchFound preg_match_all("/\b(" implode($badWords,"|") . ")\b/i"$string$matches);

        if (
$matchFound) {
         
     
              $words 
array_unique($matches[0]);
         
     
              
foreach($words as $word) {
         
         $this->set_message('badword''Your message has a band word <b>' $word '</b> you can not use that word.');
         
     }

         
     return false;
        
        } else {

            return 
true;
        }

    }

Hey Wolfgang,

I can't help you with a solution for multiple messages.
What I can say is that your for loop is setting the message for 'badword' over and over again. So you probably get only one mesage with the last banned word in it.

Why not iterate through the matches and build a string with all the found bad words? Say $found_words becomes something like 'ban, bad'.
Then you can set a message like: 'You have used one or more banned words. You can't use $found_words. Sorry!'

Hope it helps!

-Roger
I think using set_message in a loop overwrite the previous message.
So you must be getting only the message for the last bad word.

I would try to implode the word array to test.

PHP Code:
$this->set_message('badword''Your message has band word(s) <b>' implode(',',$words) . '</b> you can not use that word.'); 
Yeah, that's basically what I said...  Tongue
Rough try, not tested see if it will get you started.

Old saying get it to work first then throw in all the bells and whistles.

PHP Code:
class MY_Form_validation extends CI_Form_validation {

 
   public function badword($string)
 
   {
 
       // Dummy words
 
       $badWords = array("ban","bad","user","pass","stack","name","html");

 
       $badFound '';
 
       $badCount 0;

 
       $matches  = array();
 
       $matchFound preg_match_all("/\b(" implode($badWords,"|") . ")\b/i"$string$matches);

 
       if ($matchFound)
 
            
              $words 
array_unique($matches[0]);
 
             
              foreach
($words as $word)
 
             {
 
                 $badFound =. $word ' ';
 
                 $badCount++;
 
             }
 
             
              if 
($badCount == 1)
 
             {
 
                 $this->set_message('badword''Your message has a band word <b>' $badFound '</b>you can not use that word.');
 
             }
 
             else
              
{
 
                 $this->set_message('badword''Your message has a band words <b>' $badFound '</b>you can not use those words.');
 
             }

 
             return false       
        
}
 
       else
        
{
 
           return true;
 
       }
 
   }

Looks like the solution of InsiteFX will do the trick, but there is one tiny thing you have to change.

In the else section where the message is set for multiple bad words, the message has to be set to 'badword' and not 'badwords', since your validation function is called badword.

Hope this helps!

-Roger
(09-30-2016, 04:17 AM)InsiteFX Wrote: [ -> ]Rough try, not tested see if it will get you started.

Old saying get it to work first then throw in all the bells and whistles.

PHP Code:
class MY_Form_validation extends CI_Form_validation {

 
   public function badword($string)
 
   {
 
       // Dummy words
 
       $badWords = array("ban","bad","user","pass","stack","name","html");

 
       $badFound '';
 
       $badCount 0;

 
       $matches  = array();
 
       $matchFound preg_match_all("/\b(" implode($badWords,"|") . ")\b/i"$string$matches);

 
       if ($matchFound)
 
            
              $words 
array_unique($matches[0]);
 
             
              foreach
($words as $word)
 
             {
 
                 $badFound =. $word ' ';
 
                 $badCount++;
 
             }
 
             
              if 
($badCount == 1)
 
             {
 
                 $this->set_message('badword''Your message has a band word <b>' $badFound '</b>you can not use that word.');
 
             }
 
             else
              
{
 
                 $this->set_message('badwords''Your message has a band words <b>' $badFound '</b>you can not use those words.');
 
             }

 
             return false       
        
}
 
       else
        
{
 
           return true;
 
       }
 
   }



Thank you
(09-30-2016, 02:20 AM)salain Wrote: [ -> ]I think using set_message in a loop overwrite the previous message.
So you must be getting only the message for the last bad word.

I would try to implode the word array to test.

PHP Code:
$this->set_message('badword''Your message has band word(s) <b>' implode(',',$words) . '</b> you can not use that word.'); 

Thank you
@wolfgang1983

PHP Code:
// should this
$this->set_message('badword''Your message has a band word <b>' $badFound '</b>you can not use that word.');

// be like this?
$this->set_message('badword''Your message has a bad word <b>' $badFound '</b>you can not use that word.'); 

The first one is yours but it says band word.

Should it not be bad word?