0

所以这是我的代码:

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"]);

据我所知,每个变量都应采用指定名称的 post 变量,然后将其传递给 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”
4

1 回答 1

0
  1. 每次运行函数时都会重新定义列表,这可以避免使用静态
  2. 为了更好地了解什么转化为我更喜欢只使用一个数组并使用 array_keys 来获取 $search
  3. 您确定要替换完整的字符串“•”而不是单独替换每个字符串吗?

我应该像这样写我的函数:

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);
}
于 2012-06-28T17:59:32.077 回答