-2

I use validate php function to filter badword. And my problem is this script can't count badword in a statement that I've input. How to count badword in a statement..?

For example: You're badword1 and they badword2.
It's supposed to be 2 badword in that sentence.

PHP

function validasi($string,$banned_words) {
    foreach($banned_words as $banned_word) {
        if(stristr($string,$banned_word)){
            return false;
        }
    }
    return true;
}
$banned_words = array('badword1','badword2','badword3','badword4','badword5','badword6','badword7');
$teks = $_POST['teks'];
if (!validasi($teks,$banned_words)) {
    echo count(!validasi($teks,$banned_words));
    echo 'blocked!';
}else{
    echo 'Text valid';
}

HTML

<form action="validasi.php" method="POST">
  <input type="text" name="teks">
  <input type="submit" value="Validasi">
</form>

Output

1 !blocked.

Expected Result

2 !blocked

Tatif Isu
  • 33
  • 1
  • 1
  • 9
  • `!validasi($teks,$banned_words)` is either `true` or `false`. http://php.net/count: *If the parameter is not an array or not an object with implemented [Countable](http://php.net/manual/en/class.countable.php) interface, 1 will be returned.* – melpomene Jun 05 '16 at 08:14
  • Your validasi function is only returning a Boolean value (true/false), and "true" boolean values are printed as "1" in PHP. You will need to instead track the number of counts within that function, and then print that count instead. – Terry Jun 05 '16 at 08:14
  • If the input is "*badword1 and badword1*", should the result be `1` or `2`? – melpomene Jun 05 '16 at 08:15
  • Possible duplicate of [PHP Count Array Elements](http://stackoverflow.com/questions/4914175/php-count-array-elements) – toesslab Jun 05 '16 at 08:28
  • it's different.. I've edited the title... – Tatif Isu Jun 05 '16 at 08:36

2 Answers2

1

Your script also don't count extra the words if they are using the same word multiple times in a string. Below here is a script i should use.

function getBadWords(){
    $db_con = new PDO('dsn', 'use', 'pass'); // You PDO connection
    $query = $db_con->prepare("SELECT * FROm tb_bannedwords");
    $query->execute();
    $return = array();
    while($row=$query->fetch(PDO::FETCH_OBJ)) {
        $data = $return[] = $row->banned_words;
    }
    return $return;
}
function validate($string){
    $banned_words = getBadWords();
    $count = 0;
    foreach($banned_words as $banned_word){
        $wordCount = substr_count($string, $banned_word);
        if($wordCount > 0){
            $count = $count + $wordCount;
        }
    }
    return $count;
}
$teks = 'You\'re badword1 and they badword2 with badword1';
if(validate($teks) > 0){
    echo validate($teks) . ' blocked!';
}else{
    echo 'Text valid';
}
Paules
  • 540
  • 1
  • 3
  • 13
  • Thank you... But I have a question.. If I get data from database,, how to change it to array? `$query = $db_con->prepare("SELECT * FROm tb_bannedwords"); $query->execute(); $return= array(); while($row=$query->fetch(PDO::FETCH_OBJ)) { $data = $return[] = $row->banned_words; } $count = 0; foreach($data as $banned_word){ $wordCount = substr_count($string, $banned_word); if($wordCount > 0){ $count = $count + $wordCount; } } return $count; }` – Tatif Isu Jun 05 '16 at 09:40
-1

do this

function validasi($string,$banned_words) {
$badWordCount = 0;
foreach($banned_words as $banned_word) {
if(stristr($string,$banned_word)){
$badWordCount++;
}
}
return $badWordCount;
}
$banned_words = array('badword1','badword2','badword3','badword4','badword5','badword6','badword7');
$teks = $_POST['teks'];
$badWordsCount2 = validasi($teks,$banned_words);
if ($badWordsCount2 != 0) {
echo $badWordsCount2;
echo 'blocked!';
}else{
echo 'Text valid';
}
prabodhtiwari
  • 208
  • 1
  • 2
  • 9