有没有办法在我的数据库中删除 comma/s?代码点火器 PHP

Is there a way to remove comma/s in my database? Codeigniter PHP

如何从我的数据库中删除逗号?

我需要根据数据删除它们。 所以我有这个数据

Product 1,Product 2,,,,, - 我需要删除每个逗号

所以,我需要删除每个逗号。这是我的观点

<?php echo form_open('subjects/remove_core'); ?>

 <?php  $core_subjs = explode(',', $subjects['core_subjects']); ?>
    <?php foreach($core_subjs as $key => $subject): ?>

        <input type="hidden" name="remove_core" class="form-control" value="<?php echo $subjects['core_subjects'].'' ?>" /><br>

        <input type="text" name="coresubjs[]" class="form-control" value="<?php echo $subject ?>" /><br>
        <button type="submit" class="btn btn-success btn-sm">Save</button>

  <?php endforeach ?>
</form>

我的控制器

public function remove_core(){

    $this->subject_model->remove_core();

    redirect('subjects/edit/'.$_SESSION['editID']);
}

我的模型 - 所以在这里我不确定我这样做是否正确,因为结果不正确。

public function remove_core(){
        $data = array(
            'core_subjects' => " "
        );

        $this->db->where('id', $this->input->post('coresubjID'));
        return $this->db->update('subjects', $data);
}

我如何爆破数据

public function update_core(){
        $data = array(
            'core_subjects' => implode(',',$this->input->post('coresubjs[]'))
        );

        $this->db->where('id', $this->input->post('coresubjID'));
        return $this->db->update('subjects', $data);
}

在您的 update_core function 中,您需要 trim 额外的 ','。您可以按照以下方式执行此操作 -

public function update_core(){
        $data = array(
            'core_subjects' => rtrim(implode(',', $this->input->post('coresubjs[]')), ','); // remove the extra ',' to the right
        );

        $this->db->where('id', $this->input->post('coresubjID'));
        return $this->db->update('subjects', $data);
}

另外,请记住,您不必在每次更新时都删除数据。更新时它会自动覆盖以前的值。所以你的 remove_core 方法是多余的,应该删除

要删除进入数据库的多余逗号,您可以根据自己的喜好使用 array_filtertrimpreg_replace

$this->input->post('coresubjs[]')的理论值为

array(
   '',
   '',
   '0',
   'A',
   'B',
   '',
   'D',
   '',
   '',
);

仅使用 implode(',') 将导致

,,0,A,B,,D,,

免责声明

但是请记住,post 值中的任何逗号都不会被正确考虑,应该使用其他方式进行转义。例如:1,000 将分解为 array("1", "000")。出于这个原因,我建议重构以支持使用 json_encode()json_decode()serialize()unserialize(),而不是 implode(',')explode(',')

示例:https://3v4l.org/I2ObY


array_filter

implode 数组参数中删除 "empty" 值 (0, "", false, null)。 这将适用于任何空输入值,包括数组的中间部分。

implode(',', array_filter($this->input->post('coresubjs[]')))

结果
请注意,所有无关的逗号和 0 值都已删除

A,B,D

要避免 "empty" 值(例如 0, false)的问题,您可以改用自定义回调。

implode(',', array_filter($this->input->post('coresubjs[]'), function($value) {
    return null !== $value && '' !== $value;
}))

结果
注意所有多余的逗号都已删除

0,A,B,D

trim

仅从 implode 值中删除前导和尾随逗号。

trim(implode(',', $this->input->post('coresubjs[]')), ',')

结果
注意前导和尾随逗号已被删除,但中间的额外逗号保留了下来。

0,A,B,,D

preg_replace

类似于 trim,从 implode 值中删除前导和尾随逗号,并用单个逗号替换中间的任意 2 个或更多逗号。

preg_replace(['/,{2,}/', '/^,+/', '/,+$/'], [',', '', ''], implode(',', $this->input->post('coresubjs[]')))

图案说明:

  • ,{2,} 任意 2 个或更多逗号
  • ^,+ 以 1 个或多个逗号开头
  • ,+$ 以 1 个或多个逗号结尾

结果
注意所有多余的逗号已被删除

0,A,B,D