Jump to content

Recommended Posts

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;        
    }
?>

Link to comment
https://forums.phpfreaks.com/topic/155553-check-for-word-within-a-string/
Share on other sites

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;
}
?>

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!

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;        
    }

?>

 

 

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.

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... 

<?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 :)

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.