2012-04-25 77 views
-1

我得到了這樣簡單的代碼被輸入:塊「和「從輸入字段

private function edit_keywords($text) 
{ 
    $tresc = str_replace("\n","",$text); 
    $tresc = str_replace("\r","",$text); 
    if(strpos($text,'"')!==FALSE) 
    { 
     array_push($this->warnings,"Not allowed character found in keywords \"."); 
     return; 
    } 

目前這將阻止」被進入,我想阻止'了。這個怎麼做 ?

更換'也沒關係。

+0

這是允許的字符一個非常奇怪的限制。爲什麼你只是禁止在ASCII中找到的引號字符? – Quentin 2012-04-25 13:18:11

+1

我不知道該說些什麼。你有檢查''''的代碼,你不能用同樣的方法來檢查'''嗎?你有替代'\ n'和'\ r'的代碼,不能用同樣的代替'' '? – JJJ 2012-04-25 13:19:02

+1

只要記住:所有的客戶端檢查都很容易被忽略,所以所有驗證都必須在服務器上重新檢查 – 2012-04-25 13:22:04

回答

-1

您可以使用雙引號("'")或轉義單引號'\''來編寫第二個strpos檢查單引號。

if(strpos($text,'\'')!==FALSE) 
{ 
    array_push($this->warnings,"Not allowed character found in keywords \"."); 
    return; 
} 

最終,你可能會達到您想要儲存所有你想要在一個陣列,以測試字符的複雜程度,並在它們之間迭代,而不是不斷地追加新if聲明:

$bad_chars = array('"', '\''); 

foreach ($bad_chars as $bad_char) { 
    if (strpos($text, $bad_char) !== false) { 
    array_push($this->warnings,"Not allowed character found in keywords \"."); 
    return; # or break, to stop after the first disallowed characte 
    } 
} 
+0

謝謝。它的工作! – Spacedust 2012-04-25 13:26:49

0

試試這個:

<?php 

private function edit_keywords($text) 
{ 
    $tresc = str_replace("\n","",$text); 
    $tresc = str_replace("\r","",$text); 

    if ((strpos($text,'"')!==FALSE)||(strpos($text,"'")!==FALSE)) 
    { 
     array_push($this->warnings,"Not allowed character found in keywords \"."); 
     return; 
    } 

?> 
+0

我認爲你的邏輯與('&&')應該是一個或('||') – 2012-04-25 13:18:58

+0

當然你想要||而不是&& – Nick 2012-04-25 13:20:01

+0

@AdamBatkin Ooooops!真的,只需修復它!:-) – 2012-04-25 13:20:03