Frank
Frank

Reputation: 1864

Php check if string contains multiple words

I have looked around the internet for something that will do this but it will only work with one word.

I am trying to build a script that will detect a bad username for my site, the bad username will be detected if the username contains any of the words in an array.

Here's the code I made, but failed to work.

$bad_words = array("yo","hi");
$sentence = "yo";

if (strpos($bad_words,$sentence)==false) {
echo "success";
}

If anybody could help me, I would appreciate it.

Upvotes: 3

Views: 19330

Answers (8)

GSto
GSto

Reputation: 42350

You're close. You can do it the following way:

$bad_words = array("yo","hi");
$sentence = "yo";
$match = false;
foreach($bad_words as $bad_word) {
  if (strpos($bad_word,$sentence) === false) {  # use $bad_word, not $bad_words
    $match = true;
    break;
  }
}
if($match) { echo "success"; }

Upvotes: 0

ffdigital
ffdigital

Reputation: 11

// returns false or true
function multiStringTester($feed , $arrayTest)
{   
    $validator = false;
    for ($i = 0; $i < count($arrayTest); $i++) 
    {
            if (stringTester($feed, $arrayTest[$i]) === false ) 
            {   
                continue;
            } else 
            {
                $validator = true;              
            }       
    }
    return $validator;
}
//www.ffdigital.net

Upvotes: 1

TaZ
TaZ

Reputation: 743

I'd implement this as follows:

$bad_words = array("yo","hi");
$sentence = "yo";

$valid = true;
foreach ($bad_words as $bad_word) {
    if (strpos($sentence, $bad_word) !== false) {
        $valid = false;
        break;
    }
}

if ($valid) {
    echo "success";
}

A sentence is valid unless it contains at least one word from $bad_words. Note the strict checking (i.e. !==), without this the check will fail when the result of strpos is 0 instead of false.

Upvotes: 0

Karo
Karo

Reputation: 744

Unfortunately, you can't give an Array to functions strpos/strstr directly.

According to PHP.net comments, to do this you need to use (or create your own if you want) function like this:

function strstr_array($haystack, $needle) 
{
     if ( !is_array( $haystack ) ) {
         return false;
     }
     foreach ( $haystack as $element ) {
         if ( stristr( $element, $needle ) ) {
             return $element;
         }
     }
}

Then, just change strpos to the new function - strstr_array:

$bad_words = array("yo","hi");
$sentence = "yo";

if (strstr_array($bad_words,$sentence)==false) {
     echo "success";
}

Upvotes: 0

Willem T
Willem T

Reputation: 104

<?php
function isUserNameCorrect()
{
    Foreach($badname in $badnames)
    {
        if(strstr($badname, $enteredname) == false)
        {
             return false;
        }
    }
    return true;
}

Maybe this helps

Upvotes: 0

anubhava
anubhava

Reputation: 784898

You can use this code:

$bad_words = array("yo","hi");
$sentence = "yo you your";
// break your sentence into words first
preg_match_all('/\w+/', $sentence, $m);
echo ( array_diff ( $m[0], $bad_words ) === $m[0] ) ? "no bad words found\n" :
                                                      "bad words found\n";

Upvotes: 1

Baba
Baba

Reputation: 95103

I believe a sentence is more than one single word

Try

$badwords = array("yo","hi");
$string  = "i am yo this is testing";
$word = array_intersect( $badwords,explode(" " , $string));
if(empty($word))
{
    echo "Sucess" ;
}

Upvotes: 0

Oliver M Grech
Oliver M Grech

Reputation: 3171

use

substr_count

for an array use the following function

function substr_count_array( $haystack, $needle ) {
     $count = 0;
     foreach ($needle as $substring) {
          $count += substr_count( $haystack, $substring);
     }
     return $count;
}

Upvotes: 7

Related Questions