Jump to content

Archived

This topic is now archived and is closed to further replies.

lip9000

Check if text contains certain words

Recommended Posts

Basically I want to check a bunch of text for certain swear words, and if it contains at least 1 word, then throw out an error. All I can find on the net is replacing text with other text, I dont want to replace it, I just want to see if it's there.

 

So maybe have an array full of swear words, then after the form is submitted check if that text contains any of those words, if yes, then do something.

 

Thanks heaps in advance!

Share this post


Link to post
Share on other sites

Use the strpos() function.

$string = 'Find the word swear.';
if ( strpos($string, 'swear') !== false )
{
    echo 'I found swear!';
}

Share this post


Link to post
Share on other sites

You may want to reconsider and just replace it with !@$# characters (or whatever) as some words can contain other words (searches for "word" will find a match in the above code for instance).

 

If you want the search to be case insensitive, use stripos instead of strpos in the above code.

Share this post


Link to post
Share on other sites

So if I wanted to use multiple different words would the code go like this?

 

$string = 'Find the word swear.';
if ( stripos($string, 'swear,this,that,another,badword,andthis') !== false )
{
    echo 'I found swear!';
}

 

???????

Share this post


Link to post
Share on other sites

No, you could do something like

 

<?php
function wordsExist(&$string, $words) {
    foreach($words as &$word) {
        if(stripos($string, $word) !== false) {
            return true;
        }
    }
    return false;
}

$string = 'Find the word swear.';
if (wordsExist($string, array('swear','this','that','another','badword','andthis'))) {
    echo 'I found one of the words!';
}
?>

Share this post


Link to post
Share on other sites

You can do this if you want:

 

// Array of Bad words
$words = array('fuck','shit','damn','ass');
// Array of extention to words
$exten = array('','ed','ing','s');
// Input string
$str = 'this is a fucking sentence';
// Create an array from input
$string = explode(' ',strtolower($str));
// Create a new array for all combinations of swear words
$wordList = array();
// Add all combinations to the new array
foreach($words as $word){
   foreach($exten as $ext){
      $wordList[] = $word.$ext;
   }
}
// Loop through each input word, and check if it is a bad word or not
// FALSE = Good Words
// TRUE = Bad Words
$badWord = FALSE;
foreach($string as $s){
   if(in_array($s, $wordList)){
      $badWord = TRUE;
   }
}
// Do something if output is good or bad in this case display a message
if($badWord)
   echo 'This input contains inapropriate content';
else
   echo 'This is valid input!';

Share this post


Link to post
Share on other sites

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