inspireddesign Posted April 24, 2009 Share Posted April 24, 2009 Hello all! I have a simple function that sends back a response whether the word entered into the input box is allow or not. my problem is that I want to check if the word has that single word in the string. For example: The user enters "A FREE weekend at Knox County Lodge" I would like my function to see that the word FREE is NOT allowed and to return an error. Again, I have this working for a single word but that's the only word I can enter in the input. Once I hit the space bar it returns that the word is okay. Here's the related code. I tried making it work but obviously I'm running into problems. The comment "this -->>" is what I was trying to get work. Thanks for the help in advance. <?php $message = ''; $error = array(); // we'll fake the database check, the following spam names won't be allowed. $bad_spams = array( 'free', 'tattoos', 'get it now', 'marketing', '!' ); if ($_REQUEST['action'] == 'check_spam' && isset($_SERVER['HTTP_X_REQUESTED_WITH'])) { // means it was requested via Ajax echo json_encode(check_spam($_REQUEST['spam'])); exit; // only print out the json version of the response } function check_spam($spam) { global $bad_spams; $resp = array(); //this ->> $spam = strpos($spam, $bad_spams); if (!$spam) { $resp = array('ok' => false, 'msg' => "Enter subject line"); //this ->> } else if ($spam === false) { } else if (in_array($spam, $bad_spams)) { $resp = array("ok" => false, "msg" => "Word/Phrase you entered may be considered spam: $spam"); } else { $resp = array("ok" => true, "msg" => "<img src=\"cmrk.jpg\" height=\"16\" width=\"16\" />"); } return $resp; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155553-check-for-word-within-a-string/ Share on other sites More sharing options...
Bauer418 Posted April 24, 2009 Share Posted April 24, 2009 The below code is just an example of how to do it. I trust you can adapt it to your needs: <?php $spamList = array('word1', 'word2', 'word3'); function checkSpam( $input ) { global $spamList; foreach ( $spamList as $spamWord ) { if ( stripos( $check, $spamWord ) !== false ) return strtolower( $spamWord ); } return false; } if ( $badWord = checkSpam( 'This should detect word1 spam') ) { print 'The following word is not allowed: ' . $badWord; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155553-check-for-word-within-a-string/#findComment-818593 Share on other sites More sharing options...
mikesta707 Posted April 24, 2009 Share Posted April 24, 2009 ok if i have this right you want to check if a certain string is in a string your user submitted. we can use the stristr() function for that (which checks if a string is in a string, and returns whats after the string if it is, and false if it is not) so take the following global $bad_spams; foreach ($bad_spams as $bad){ if (stristr($string, $bad) !== false){ //do whatever it is you want to do if the spam is found! } that should do what you want it to. hope that helped! Quote Link to comment https://forums.phpfreaks.com/topic/155553-check-for-word-within-a-string/#findComment-818598 Share on other sites More sharing options...
inspireddesign Posted April 24, 2009 Author Share Posted April 24, 2009 Thanks a bunch for the help! I think we are on the right track. I've included the snippet of code you provided but it seems to hang when I get to a "bad word" in the array. If I type "The world is FREE of bugs". Everything checks out until I get to the word FREE and then it doesn't return the word. I have a nifty json script I'm running that will supply a check mark if it returns true and once it returns false the warning msg should appear with the bad word. I believe my syntax is wrong in below IF statement. Can you help? <?php function check_spam($input) { global $spamList; $resp = array(); $check = strtolower( $input ); foreach ( $spamList as $spamWord ) { if ( strpos( $check, strtolower( $spamWord ) ) !== false ) return strtolower( $spamWord ); } if (!$input) { $resp = array('ok' => false, 'msg' => "Enter subject line"); } else if ($spamWord == false) { $resp = array("ok" => false, "msg" => "Word/Phrase you entered may be considered spam: $spamWord"); } else { $resp = array("ok" => true, "msg" => "<img src=\"cmrk.jpg\" height=\"16\" width=\"16\" />"); } return $resp; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155553-check-for-word-within-a-string/#findComment-818640 Share on other sites More sharing options...
.josh Posted April 24, 2009 Share Posted April 24, 2009 I haven't read the rest of the thread yet. Just got up to the I would like my function to see that the word FREE is NOT allowed and to return an error. Again, I have this working for a single word but that's the only word I can enter in the input. Once I hit the space bar it returns that the word is okay. in your OP. trim the posted input. Quote Link to comment https://forums.phpfreaks.com/topic/155553-check-for-word-within-a-string/#findComment-818650 Share on other sites More sharing options...
inspireddesign Posted April 25, 2009 Author Share Posted April 25, 2009 in your OP. trim() the posted input. I'm not sure I understand? Can someone give a better explanation? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/155553-check-for-word-within-a-string/#findComment-818780 Share on other sites More sharing options...
.josh Posted April 25, 2009 Share Posted April 25, 2009 You said if you enter "FREE" it works, but if you enter "FREE " it does not. In your code, trim it. Quote Link to comment https://forums.phpfreaks.com/topic/155553-check-for-word-within-a-string/#findComment-818782 Share on other sites More sharing options...
inspireddesign Posted April 25, 2009 Author Share Posted April 25, 2009 I tried that and it didn't work. It's not after the space, it's when the word is caught in the array. So if I type, FRE we are good but as soon as I type the trailing E it doesn't return the error. It does this for any of the words or phrases in the array. It seems to catch it but doesn't return the error. <?php $spamList = array( 'free', 'tattoos', 'get it now', 'marketing', '!' ?> <?php function check_spam($input) { global $spamList; $resp = array(); $check = strtolower( $input ); foreach ( $spamList as $spamWord ) { if ( strpos( $check, strtolower( $spamWord ) ) !== false ) return strtolower( $spamWord ); } if (!$input) { $resp = array('ok' => false, 'msg' => "Enter subject line"); } else if ($spamWord == false) { $resp = array("ok" => false, "msg" => "Word/Phrase you entered may be considered spam: $spamWord"); } else { $resp = array("ok" => true, "msg" => "<img src=\"cmrk.jpg\" height=\"16\" width=\"16\" />"); } return $resp; } ?> If you notice there's "get it now" in the array. I don't think trim will do it... Quote Link to comment https://forums.phpfreaks.com/topic/155553-check-for-word-within-a-string/#findComment-818799 Share on other sites More sharing options...
RussellReal Posted April 25, 2009 Share Posted April 25, 2009 <?php class SpamFilter { private $words; public function __construct($wordlist) { $this->words = $wordlist; } private function prep($input) { return trim(preg_replace(array('/[^a-z0-9 \n\.\,-]/i','/\n/','/[\,\.]/'),array('',' ',"\n"),$input)); } public function spam_rating($input) { $input = $this->prep($input); $exp = explode("\n",$input); $length = 0; $count = 0; foreach ($exp as $line) { $length += substr_count(trim($line),' ') + 1; foreach ($this->words as $word) { if (stripos($line,$word) !== false) $count += substr_count(trim(str_replace(array('-','_'),' ',$word)),' ') + 1; } } return min(100,floor((($count * 3.5) / $length) * 100)); } } $s = new SpamFilter(array('free','sign-up','sign up','amazing','unbelievable')); echo $s->spam_rating('This unbelievable free offer is all yours, all you need to do is go to my website, sign up, and set up your mailing address to receive your AMAZING offer!').'%'; // the above echo returns 58%, each 'flag' word is worth 350% an average word.. enjoy ?> I put this class together for you, just to help you out Quote Link to comment https://forums.phpfreaks.com/topic/155553-check-for-word-within-a-string/#findComment-818858 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.