Jump to content

[SOLVED] match words wont match why.


redarrow

Recommended Posts

<?php

$x="see you soon redarrow";
$words=array("a","see","c");
$word = explode(" ", $x);

   for($i = 0; $i < count($word); $i++){

      if (in_array($word[$i], $words)){
         echo "$word[$i] - Bad word!<br>";
      } else {
         echo "$word[$i] - Good Word!<br>";
      }

   }
?>

 

EDIT:

It was because you were trying to match words to your whole string of words. You had to split up your string then check against every individual word...

Link to comment
Share on other sites

 

 

can u kindly post a proper example to cheek if a word exist in a

message please the below last posted example driven me mad cheers.

 

there must ba a simple way to see if a bad word exists in a set condition.

<?php


$message="your all  s and all have  you ";

$bad_words=array("c","b","s");

$x=explode(" ",$message);

for($i=0; $i<count($x); $i++){

$t=in_array($x[$i], $bad_words);

if($t==true){
	echo "bad word";
	}else{
	echo "good word";


}
}

?>

Link to comment
Share on other sites

example the loop wont work

 

<?php

$x="you soon  see redarrow";

$word=array("a","see","c");

$message=explode(" ",$x);

for($i=0; $i < count ($message); $i++){

if(in_array($message[$i], $word)){

echo"You entred a bad word!";
exit;	

}else{

echo "Word is good!";	
exit;
}
}
?>

Link to comment
Share on other sites

try this

 

<?

$x=" you soon redarrow";

$word=array("a","see","c");

 

$message=explode(" ",$x);

 

for($i=0; $i < count ($message); $i++){

 

if(in_array($message[$i], $word)){

 

echo"You entred a bad word!";

exit;

 

}else{

 

echo "Word is good!";

exit;

}

}

?>

Link to comment
Share on other sites

nope i added see to the message no luck

<?php
$x=" you soon see redarrow";
$word=array("a","see","c");

$message=explode(" ",$x);

for($i=0; $i < count ($message); $i++){

if(in_array($message[$i], $word)){

echo"You entred a bad word!";
exit;   

}else{
      
echo "Word is good!";   
exit;
}
}
?>

 

 

tried this aswell

<?php

$x="you soon  see redarrow";

$word=array("a","see","c");

$message=explode(" ",$x);

for($i=0; $i < count ($message); $i++){

if(strchr($message[$i],$word)){

echo"You entred a bad word!";


}else{

echo "Word is good!";	

}
}
?>

Link to comment
Share on other sites

this wont work iver

 

<?php

$x="you soon see redarrow";

$word=array("a","see","c");

$message=explode(" ",$x);


foreach($message as $d){

foreach($word as $c){

if (strstr($d, $c)){

echo"You entred a bad word!";
exit;

}else{

echo "Word is good!";	
exit;
}
}
}
?>

Link to comment
Share on other sites

try

<?php
function bad_word($string, $array_of_bad_words, $bad_words_find = array()) {
$out = true;
$bad_words_find = array();
$bad_words_find = array_intersect(explode(' ',$string),$array_of_bad_words);
if (count($bad_words_find)) return true; else return false;
}

$x="you soon see redarrow c c";

$word=array("a","see","c");

if (bad_word($x,$word)) echo "You entred a bad word!"; else echo "Word is good!";	
// or
echo '<hr />';
$a = array();
bad_word($x, $word, &$a);
echo 'Bad words is: ',implode(', ', $a);
?>

Link to comment
Share on other sites

No need to change your code completely. The code snippet you provided in your first post is nearly there. try this:

<?php

$str = "see you soon redarrow";

$words = explode(' ', $str);

$bad_words = array("a", "see", "c");

foreach($words as $word)
{
    if(in_array($word, $bad_words))
    {
        echo "'$word' - is a bad word!<br />";
    }
    else
    {
        echo "'$word' - is a good word!<br />";
    }
}

?>

Link to comment
Share on other sites

wont work like this why please

 

<?php

$str = "you see redarrow";

$words = explode(' ', $str);

$bad_words = array("a", "see", "c");

foreach($words as $word)
{
   if(in_array($word, $bad_words))
   {
       echo "bad word";
       exit;
   }
   else
   {
       echo "good word";
       exit;
   }
}

?>

Link to comment
Share on other sites

Is this what you mean:

<?php

$str = "see you soon redarrow";

$words = explode(' ', $str);

$bad_words = array("a", "see", "c");

$bad_words_used = 0;

foreach($words as $word)
{
    // check whether bad words have been used in string
    // if they are we increment the 'bad_words_used' variable by one each time a bad word is used
    if(in_array($word, $bad_words))
    {
        $bad_words_used++;
    }
}

// check to see if 'bad_words_used' variable is greater than 0
// if its greater than 0 we kill script display an error
// if its not greater than 0 we add to database
if($bad_words_used > 0)
{
    die('Bad words have been used');
}
// else badwords have not been used.
else
{
    echo 'No bad words have been used we add to database now';
}

?>

 

Link to comment
Share on other sites

Thank you so much wildteen it what i wanted all day your so good.

 

I will trie to learn this now properly.

 

 

if a user submit a bad word then the user is given a echoed

message else database get's updated thank you.

 

 

I trie to pm another user but the pm dosent work can u fix it please wildteen cheers.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.