dos8410 2014-01-26 20:36
浏览 47
已采纳

根据数组键名删除post值中的引号

I want to be able to remove quotations from a field on or abouts the name of 'quote'. On post, all my field names and values get matched up and put into an array then enter to the database. Before the SQL is built and after I build the value-key array, how can I single out the field quote, remove the quotation marks that the user inputted, and then add/keep the content in the $values array for my SQL? The questionable area starts with the comment "remove quotes"

public function insertIntoDb($table, $carryUrl = NULL, $ext = '')
{
    if (in_array($table, $this->disallow_insert)) {
        self::show_error("Inserting into the table '{$table}' is not possible, check the configuration file if this is an error.");
    } elseif (!isset($table)) {
        self::show_error('Missing `table` parameter in ' . __FUNCTION__);
    }
    $resultInsert = Nemesis::query("SHOW COLUMNS FROM {$table}");
    if (!$resultInsert) {
        self::show_error(QUERY_ERROR);
    }
    $fieldnames = array();
    if ($resultInsert->num_rows > 0) {
        while ($row = $resultInsert->fetch_array()) {
            $fieldnames[] = $row['Field'];
            $values = array_intersect_key($_POST, array_flip($fieldnames));
            // $values = array_filter($values, function($x) { return $x !== ''; });
            // <5.3 $values = array_filter($values, create_function('$x', 'return $x !== "";'));
        }
    }
    // remove quotes for testimonials
    if (array_key_exists('quote', array_change_key_case($values, CASE_LOWER))) {

        $values['quote'] = preg_replace("/<!--.*?-->/", "", $values); // remove quotes

    }
    // filter the array
    $values = self::filter($values);
    $sql = sprintf("INSERT INTO %s (created, created_by, %s) VALUES (NOW(), '$_SESSION[user_id]', '%s')", $table, implode(', ', array_keys($values)), implode("', '", $values));
    if ($this->debug) {
        echo '<p>' . $sql . '</p>';
    } elseif (Nemesis::query($sql)) {
        $msg = new Messages();
        $msg->add('s', QUERY_INSERT_SUCCESS);
        if ($table == 'projects') {
            $msg = new Messages();
            $msg->add('s', "Information was added to the database. Time to add images!");
        }
        if (!is_null($carryUrl) && isset($carryUrl)) {
            redirect($carryUrl . '?id=' . $_POST['id'] . '&table=' . $table . $ext);
        }
    } else {
        self::show_error(QUERY_ERROR);
    }
}
  • 写回答

1条回答 默认 最新

  • doujia7779 2014-02-04 17:44
    关注

    preg_replace is a function that returns a value, not a void. You'll need to assign the returned value back to $values['quote']:

    // remove quotes for testimonials
    if (array_key_exists('quote', array_change_key_case($values, CASE_LOWER))) {
    
        $values['quote'] = preg_replace("/(\"|')/", "", $values['quote']); // remove quotes
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题