2012-06-28 119 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)一样 –

相关问题