php
2011-03-10 18 views 0 likes 
0

例如,我有這個字符串"this is a testing string",我想測試天氣詞「串」在那裏。 我將如何在PHP中做到這一點?有沒有辦法在php中檢查字符串中的匹配,比如mysql中的「like」?

$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'"); 

} 

回答

1
1

您可以使用preg_match();或 ...

會更容易些,因爲它不會涉及到正則表達式,但呵呵,實驗,看看有什麼適合你最好!

好了,讓我們開始...
1)您將通過把所有這些「壞」字陣列內,像賺了不少:

$badwords = array('badword1', 'badword2', 'badwords3'); // and so on... 

2)讓我們通過所有的BADWORDS運行,檢查他們的消息中的位置,有一個循環:

$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. 
} 

玩得開心!

P.S.我不知道這是否有效,目前無法測試,但我認爲應該。

http://php.net/manual/en/function.preg-match.php
http://php.net/manual/en/function.strpos.php

+0

嘿。它是這樣的,我使用這個strpos爲我的web應用程序發表評論。所以舉例來說,每當用戶在他的評論中有不好的單詞時,數據庫中的表中的標誌列將具有值1.標誌默認情況下都初始化爲零。所以我所做的是將所有不良詞語存儲到$ s1,$ s2,$ s3 ....等等。並在使用strpos提交評論時比較所有這些。但它不起作用。我的方法錯了嗎? – user628961 2011-03-11 07:30:03

+0

@ user628961,請爲此發佈整個腳本,可能有很多原因導致它無法正常工作。而且,只要你得到你想要的結果,除了一些課程外,沒有對錯的方法。所以,是的,請提交處理這些文字的腳本。這將節省很多時間! – jolt 2011-03-11 09:00:12

+0

我張貼我的代碼上面。提前致謝。 – user628961 2011-03-11 09:22:44

4

如果它是一個直接匹配,你可以這樣做:

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


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

然而,因爲在字符串中的位置可以是零的情況下,你需要做的!== FALSE方法精確解決所有病例

+0

嘿。它是這樣的,我使用這個strpos爲我的web應用程序發表評論。所以舉例來說,每當用戶在他的評論中有不好的單詞時,數據庫中的表中的標誌列將具有值1.標誌默認情況下都初始化爲零。所以我所做的是將所有不良詞語存儲到$ s1,$ s2,$ s3 ....等等。並在使用strpos提交評論時比較所有這些。但它不起作用。我的方法錯了嗎? – user628961 2011-03-11 07:30:38

1
$haystack = "this is a testing string."; 
$needle = "string"; 
$found = strstr($haystack, $needle); 

if($found!==FALSE) 
    echo "I found it!"; 
相關問題