有没有一种方法可以在php中检查字符串中的匹配项,比如;例如;在mysql中?

有没有一种方法可以在php中检查字符串中的匹配项,比如;例如;在mysql中?,php,Php,例如,我有一个字符串“这是一个测试字符串”,我想测试单词“string”是否在其中。 在PHP中如何实现这一点 $theMessage = addslashes(strip_tags($_POST['message'])); $s1='binuang'; $s2='ilad'; $s3='fraud'; $s4='fake'; $s5='ayaw mo ug tuo'; $s6='pangilad'; $s7='bot2'; $s8='bot-bot'; $s9='botbot'; $s10=

例如,我有一个字符串
“这是一个测试字符串”
,我想测试单词“string”是否在其中。 在PHP中如何实现这一点

$theMessage = addslashes(strip_tags($_POST['message']));

$s1='binuang';
$s2='ilad';
$s3='fraud';
$s4='fake';
$s5='ayaw mo ug tuo';
$s6='pangilad';
$s7='bot2';
$s8='bot-bot';
$s9='botbot';
$s10='di tinuod';
$s11='di ni tinuod';

$t1=strpos($theMessage, $s1);
$t2=strpos($theMessage, $s2);
$t3=strpos($theMessage, $s3);
$t4=strpos($theMessage, $s4);
$t5=strpos($theMessage, $s5);
$t6=strpos($theMessage, $s6);
$t7=strpos($theMessage, $s7);
$t8=strpos($theMessage, $s8);
$t9=strpos($theMessage, $s9);
$t10=strpos($theMessage, $s10);
$t11=strpos($theMessage, $s11);

if($t1===true || $t2===true || $t3===true || $t4===true || $t5===true || $t6===true || $t7===true || $t8===true || $t9===true || $t10===true || $t11===true)
{

$sql2=mysql_query("UPDATE property SET valid='1' where p_id='$q'");

}

使用
strpos

您可以使用
preg_match()
strpos()

strpos()会更容易,因为它不会涉及正则表达式,但是,嗯,试试看什么最适合你

好的,让我们开始…
1) 如果将所有这些“坏”字放入数组中,您将获得很多收益,如:

$badwords = array('badword1', 'badword2', 'badwords3'); // and so on...
2) 让我们通过一个循环来浏览所有的坏话并检查它们在消息中的位置:

$found = 0; // just a simple counter, to populate if a bad word is found..
foreach($badwords as $word){ // for each "bad word" from array, used in loop as "$word"
    if(strpos($theMessage, $word) !== 'false'){ // if we've found the word...
        $found++; // ...plus one for found
    }
}
3) 查询部分。。。我们检查是否发现了坏单词,如果是,则显示错误,否则继续查询:

if($found > 0){ // self explanatory...
    echo 'Error, bad words found, you shall not pass!';
}else{
    $query = mysql_query("UPDATE `property` SET `valid` = '1' WHERE `p_id` = '{$q}';"); // {$q} == $q, just formatting preference.
}
4) 总之,只是为了好玩和回答的长度:

$theMessage = addslashes(strip_tags($_POST['message']));
$badwords = array('badword1', 'badword2', 'badwords3'); // and so on...

$found = 0; // just a simple counter, to populate if a bad word is found..
foreach($badwords as $word){ // for each "bad word" from array, used in loop as "$word"
    if(strpos($theMessage, $word) !== 'false'){ // if we've found the word...
        $found++; // ...plus one for found
    }
}

if($found > 0){ // self explanatory...
    echo 'Error, bad words found, you shall not pass!';
}else{
    $query = mysql_query("UPDATE `property` SET `valid` = '1' WHERE `p_id` = '{$q}';"); // {$q} == $q, just formatting preference.
}
玩得开心

另外,我不知道这是否有效,目前无法测试,但我认为应该



如果是直接匹配,您可以执行以下操作:

$stringToTest="this is a testing string";
$findMe="string";


if(strpos($stringToTest,$findMe)!==FALSE){
//something
}

但是,由于字符串位置可能为零,因此需要执行以下操作==错误的方法可准确解决所有病例

hey。就像这样,我将这个strpos用于我的有注释的web应用程序。所以,例如,每当用户的注释中有坏话时,数据库中my表中的flag列的值为1。默认情况下,标志都初始化为零。所以我所做的就是将所有我认为不好的单词存储到$s1,$s2,$s3。。。。等等并在使用strpos提交评论时比较所有这些。但它不起作用。我的方法错了吗?@user628961,请发布你的整个脚本,它不起作用的原因可能有很多。而且,只要你们得到你们想要的结果,就并没有正确或错误的方法,当然也有例外。所以,是的,请在提交时发布处理这些单词的脚本。这会节省很多时间!嘿就像这样,我将这个strpos用于我的有注释的web应用程序。所以,例如,每当用户的注释中有坏话时,数据库中my表中的flag列的值为1。默认情况下,标志都初始化为零。所以我所做的就是将所有我认为不好的单词存储到$s1,$s2,$s3。。。。等等并在使用strpos提交评论时比较所有这些。但它不起作用。我的方法错了吗?
$haystack = "this is a testing string.";
$needle = "string";
$found = strstr($haystack, $needle);

if($found!==FALSE)
   echo "I found it!";