For example i have this string "this is a开发者_C百科 testing string"
, and I want to test weather the word "string" is in there.
How would I do that in 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'");
}
if it's a direct match, you can do something like:
$stringToTest="this is a testing string";
$findMe="string";
if(strpos($stringToTest,$findMe)!==FALSE){
//something
}
however, because of the case where the string position could be zero, you need to do the !==FALSE method to accurately address all cases
Use strpos
: http://php.net/manual/en/function.strpos.php
$haystack = "this is a testing string.";
$needle = "string";
$found = strstr($haystack, $needle);
if($found!==FALSE)
echo "I found it!";
You can use preg_match();
or strpos();
...
strpos();
would be easier, because it won't involve regular expressions, but huh, experiment and see what suits you best!
Okay, lets begin...
1) You would earn a lot by putting all those "bad" words inside an array, like:
$badwords = array('badword1', 'badword2', 'badwords3'); // and so on...
2) Let's run through all the badwords and check for their position in message, with a loop:
$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) The query part... We check if bad words have been found, and if yes, we show error, otherwise continue with query:
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) All together, just for fun and the length of answer:
$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.
}
Have fun!
P.S. I have no idea if this works, can't test at the moment, but I think it should.
http://php.net/manual/en/function.preg-match.php
http://php.net/manual/en/function.strpos.php
精彩评论