2012-06-28 106 views
0

因此,這裏是我的代碼:str_replace不工作 - 爲什麼會發生這種情況?

function csq($string) 
{ 
    $search = array(chr(145), 
        chr(146), 
        chr(147), 
        chr(148), 
        chr(151), 
        chr(149), 
        "•"); 

    $replace = array("'", 
        "'", 
        '"', 
        '"', 
        '-', 
        '•', 
        '•'); 

    return str_replace($search, $replace, $string); 
} 
$username = csq($_POST["username"]); 
$titletag = csq($_POST["titletag"]); 
$keywordtag = csq($_POST["keywordtag"]); 
$desctag = csq($_POST["desctag"]); 
$content = csq($_POST["content"]); 

據我所知,每個變量應採取指定名稱的變量後,再傳遞到CSQ()函數,它會替換特殊字符。

這沒有發生。我寫錯了什麼?

這裏有一個字符串:

• Went over key word list that was generated 
o Ant1 highlighted approved words 
o We should add a column to calculate the ratio between the number of queries vs. the number of results 「in parenthesis」 
+5

你能提供一個'$ string'不你打算怎麼辦? –

+0

在其中例如140-155範圍內沒有字符,所以沒有什麼可以替代 –

+0

「並且子彈應該都在字符串內。 –

回答

0
  1. 你每次運行功能, ,可以是避免使用靜態時間redifing列表
  2. 拿到過什麼翻譯的更好地瞭解什麼,我喜歡只使用一個數組 並使用array_keys獲取$ search
  3. 您確定要替換完整的字符串「?」,而不是將它們中的每一個都分開嗎?

我應該有writen喜歡我的功能:

function csq($string) 
{ 
    static $translation_table; 

    if(!$translation_table) 
    { 
     $translation_table = array(); 
     $translation_table[chr(145)] = "'"; 
     $translation_table[chr(146)] = "'"; 
     $translation_table[chr(147)] = '"'; 
     $translation_table[chr(148)] = '"'; 
     $translation_table[chr(151)] = "_"; 
     $translation_table[chr(149)] = "•"; 
     $translation_table["â"] = "•"; 
     $translation_table["€"] = "•"; 
     $translation_table["¢"] = "•"; 
    } 

    return str_replace(array_keys($translation_table), $translation_table, $string); 
} 
+0

我相信我想將它全部替換爲一個,而不是單個。它應該和chr(149)一樣 –

相關問題