Php check if string contains multiple words

安稳与你 提交于 2020-05-27 08:33:13

问题


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.


回答1:


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



回答2:


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



回答3:


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



回答4:


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



回答5:


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

Maybe this helps




回答6:


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



回答7:


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.




回答8:


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


来源:https://stackoverflow.com/questions/10518368/php-check-if-string-contains-multiple-words

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!