需要匹配此示例中的所有字词变体

时间:2018-01-02 19:39:18

标签: php arrays preg-match

这会标记bettinggamblingxxx字样,但不会标记BettingGamblingXXX。< / p>

我想要标记这些词的所有变化(即投注,赌注,投注等等)。显然,我可以单独列出它们,但我想以某种方式匹配它们,以便标记这些词的所有变体。

$original  = $_POST['message'];

$spamw = array('betting', 'gambling', 'xxx');

foreach ($spamw as $num) {
    if (( $pos = strpos( $original, $num )) !== false) { 
        $err = 'Something went wrong. Please, try again.';
    }
}

SOLOVED

$spamw = array('betting', 'gambling', 'xxx');
$original = $_POST['message'];

foreach ($spamw as $spamwords) {
  if (preg_match("/\b$spamwords\b/i", $original)) {
$err = 'Something went wrong. Please, try again.';
  }
}

3 个答案:

答案 0 :(得分:0)

好的,我用我心中所有变化的0.1%来更新溶剂:

<?php
function tospam($dictswrd){
    global $word;
    print('the imput is <span style="color:red">'.$word.'</span> compare to <span style="color:blue">'.$dictswrd.'</span> from Dictionary<br>');
    $pattern = '/(?<!\p{L})'.strtolower($word).'*/iu';
    return preg_match($pattern,$dictswrd ) >0;
}




$dictionary = array('betting', 'gambling', 'xxx'); //a small internal database loaded from a internal file maybe file?
/*
 eg:
$dictionary =file('spam_dictionary.txt');
internal you can drop a file 
spam_dictionary.txt :
---------------
betting
gambling
xxx
---------------
or you can fill the array of dictionary from a kind of database=any kind
*/

if(isset($_POST['message'])){
    print('<h1>FORM`s POST method 1</h1>');

    $word=$_POST['message'];//let's say that's coming from input from post
    $result_spam_or_not=array_filter($dictionary,'tospam' );
    print_r($result_spam_or_not);print('<hr>');

    $word=$_POST['message'];//let's say that's coming from input from post
    $result_spam_or_not=array_filter($dictionary,'tospam' );
    print_r($result_spam_or_not);print('<hr>');


    $word=$_POST['message'];//let's say that's coming from input from post
    $result_spam_or_not=array_filter($dictionary,'tospam' );
    print_r($result_spam_or_not);print('<hr>');

//or
print('<h1>FORM`s POST method 2</h1>');

$word=$_POST['message'];//let's say that's coming from input from post
$result_spam_or_not=array_filter($dictionary,'tospam' );
print(!empty($result_spam_or_not)? 'SPAM':'ok!');print('<hr>');



$word=$_POST['message'];//let's say that's coming from input from post
$result_spam_or_not=array_filter($dictionary,'tospam' );
print(!empty($result_spam_or_not)? 'SPAM':'ok!');print('<hr>');


$word=$_POST['message'];//let's say that's coming from input from post
$result_spam_or_not=array_filter($dictionary,'tospam' );
print(!empty($result_spam_or_not)? 'SPAM':'ok!');print('<hr>');


//or
print('<h1>FORM`s POST method 3</h1>');

$word=$_POST['message'];//let's say that's coming from input from post
$result_spam_or_not=array_filter($dictionary,'tospam' );
if(!empty($result_spam_or_not))print('spam');else print('not a spam');print('<hr>');



$word=$_POST['message'];//let's say that's coming from input from post
$result_spam_or_not=array_filter($dictionary,'tospam' );
if(!empty($result_spam_or_not))print('spam');else print('not a spam');print('<hr>');


$word=$_POST['message'];//let's say that's coming from input from post
$result_spam_or_not=array_filter($dictionary,'tospam' );
if(!empty($result_spam_or_not))print('spam');else print('not a spam');print('<hr>');

}


print('<h1>method 1</h1>');

$word='Gambling';//$word=$_POST['message'];//let's say that's coming from input from post
$result_spam_or_not=array_filter($dictionary,'tospam' );
print_r($result_spam_or_not);print('<hr>');

$word='gambling';//$word=$_POST['message'];//let's say that's coming from input from post
$result_spam_or_not=array_filter($dictionary,'tospam' );
print_r($result_spam_or_not);print('<hr>');


$word='blablabla';//$word=$_POST['message'];//let's say that's coming from input from post
$result_spam_or_not=array_filter($dictionary,'tospam' );
print_r($result_spam_or_not);print('<hr>');


//or
print('<h1>method 2</h1>');

$word='Gambling';//$word=$_POST['message'];//let's say that's coming from input from post
$result_spam_or_not=array_filter($dictionary,'tospam' );
print(!empty($result_spam_or_not)? 'SPAM':'ok!');print('<hr>');



$word='gambling';//$word=$_POST['message'];//let's say that's coming from input from post
$result_spam_or_not=array_filter($dictionary,'tospam' );
print(!empty($result_spam_or_not)? 'SPAM':'ok!');print('<hr>');


$word='blablabla';//$word=$_POST['message'];//let's say that's coming from input from post
$result_spam_or_not=array_filter($dictionary,'tospam' );
print(!empty($result_spam_or_not)? 'SPAM':'ok!');print('<hr>');


//or
print('<h1>method 3</h1>');

$word='Gambling';//$word=$_POST['message'];//let's say that's coming from input from post
$result_spam_or_not=array_filter($dictionary,'tospam' );
if(!empty($result_spam_or_not))print('spam');else print('not a spam');print('<hr>');



$word='gambling';//$word=$_POST['message'];//let's say that's coming from input from post
$result_spam_or_not=array_filter($dictionary,'tospam' );
if(!empty($result_spam_or_not))print('spam');else print('not a spam');print('<hr>');


$word='blablabla';//$word=$_POST['message'];//let's say that's coming from input from post
$result_spam_or_not=array_filter($dictionary,'tospam' );
if(!empty($result_spam_or_not))print('spam');else print('not a spam');print('<hr>');


?><form method="post" action="">
    spam!<input type="text" name="message" value="Gambling"><input type="submit">
</form>
<form method="post" action="">
    spam!<input type="text" name="message" value="gambling"><input type="submit">
</form>
<form method="post" action="">
    not a spam!<input type="text" name="message" value="blablabla"><input type="submit">
</form>

答案 1 :(得分:0)

执行此操作的一种方法是将$spamw数组中的所有单词存储为小写,就像您现在正在执行的那样,然后使用PHP strtolower()函数转换任何单词用户输入到小写字符。然后,您的PHP脚本可以将用户输入与存储在$spamw数组中的值进行比较,检查是否有任何单词恰好匹配。

答案 2 :(得分:0)

只需使用stripos函数,它可以找到第一次出现不区分大小写的子字符串的位置,而不是strpos