2013-03-21 12 views
0

所以我得到如何用其他單詞代替某些單詞。我試圖弄清楚的是如何接受一個詞並用短語替換它並消除所有其他輸入。PHP用短語代替發誓的單詞

例如:

不好的詞是 '狗'

用戶輸入 - > '你聞起來像一隻狗。'

而不是用'彩虹'或其他東西取代'狗',我想讓它迴應一下:'你是一個不錯的嘴'。

這裏就是我有下面的代碼:

<?php 

$find = array('dog', 'cat', 'bird'); 
$replace = 'You are a potty mouth.'; 

if (isset ($_POST['user_input'])&&!empty($_POST['user_input'])) { 
    $user_input = $_POST['user_input']; 
    $user_input_new = str_ireplace($find, $replace, $user_input); 

     echo $user_input_new; 
} 
?> 

有了這個代碼,它的回聲:「你聞起來像你是一個pottymouth。」

我確信這是一個轉貼,我很抱歉。我所能找到的所有東西都是關於如何只替換部分字符串而不是全部字符串的文檔。

+9

我希望現在可以看到一些關於這個的clbuttic迴應。 – DaveRandom 2013-03-21 15:16:44

+3

我更喜歡scunthorpe的問題... – 2013-03-21 15:18:01

+0

很難在任何情況下都能很好地工作,例如與「孔雀」,「斯肯索普」等合法詞語的部分不匹配 – 2013-03-21 15:19:00

回答

0

我最近一直在尋找同樣的問題,這裏有一個我正在努力過濾某些單詞的腳本。仍在進行中的工作,但它有能力輸出用戶消息或自定義消息。希望它能幫助你,或者指出你正確的方向。

define("MIN_SAFE_WORD_LIMIT", 3); 
$safe = true; 
$whiteList = array(); 
$blackList = array(); 

$text = 'Test words fRom a piece of text.'; 

$blCount = count($blackList); 

for($i=0; $i<$blCount; $i++) { 
    if((strlen($blackList[$i]) >= MIN_SAFE_WORD_LIMIT) && strstr(strtolower($text), strtolower($blackList[$i])) && !strstr(strtolower($text), strtolower($whiteList[$i]))) { 
     $safe = false; 
    } 
} 

if(!$safe) { 
    // Unsafe, flag for action 
    echo 'Unsafe'; 
} else { 
    echo $text; 
} 
1

那麼,在這種情況下,你可以檢查是否有一個「壞詞」,在用戶輸入的字符串,如果返回true,echo「你在一個不起眼的嘴。」

您可能需要使用strpos()

例如

if(strpos($_POST['user_input'],'dog')!==FALSE) { 
    echo('You are a potty mouth'); 
} 

如果你有一個「壞詞」數組,你需要遍歷它們來檢查用戶輸入中發生的任何事情。

0

你不想替換不好的單詞,而是整個字符串,所以你應該只匹配,如果匹配,則將整個字符串設置爲替換字符串。

另外,正如評論中指出的那樣,單詞可以是另一個有效單詞的一部分,所以如果你想考慮這一點,你應該只匹配整個單詞。

這個簡單的示例使用字邊界的正則表達式匹配的單詞(在本例中,這將是在一個循環中,循環您的壞字陣列):

foreach ($find as $your_word) 
{ 
    $search = '/\b' . preg_quote($your_word) . '\b/i'; 
    if (preg_match($search, $_POST['user_input']) === 1) 
    { 
    // a match is found, echo or set it to a variable, whatever you need 
    echo $replace; 
    // break out of the loop 
    break; 
    } 
} 
0

這裏有一個替代的解決方案,匹配單詞並替換爲* len的str。這不會匹配像斯坎索普這樣的詞,因爲它使用單詞邊界,並且您還可以添加第三個參數來揭示該單詞的第一個字母,以便您知道單詞在沒有看到的情況下說出來。

<?php 
$badwords = array('*c word', '*f word','badword','stackoverflow'); 

function swear_filter($str,$badwords,$reveal=null) { 
    //Alternatively load from file 
    //$words = join("|", array_filter(array_map('preg_quote',array_map('trim', file('badwords.txt'))))); 


    $words = join("|", array_filter(array_map('preg_quote',array_map('trim', $badwords)))); 
    if($reveal !=null && is_numeric($reveal)){ 
     return preg_replace("/\b($words)\b/uie", '"".substr("$1",0,'.$reveal.').str_repeat("*",strlen("$1")-'.$reveal.').""', $str); 
    }else{ 
     return preg_replace("/\b($words)\b/uie", '"".str_repeat("*",strlen("$1")).""', $str); 
    } 

} 


$str="There was a naughty Peacock from Scunthorpe and it said a badword, on stackoverflow"; 

//There was a naughty Peacock from Scunthorpe and it said a b******, on s************ 
echo swear_filter($str,$badwords,1); 

//There was a naughty Peacock from Scunthorpe and it said a *******, on ************* 
echo swear_filter($str,$badwords); 
?> 
相關問題