gpt4 book ai didi

php - 在 codeigniter 中如何删除字符串中不需要的字符或符号

转载 作者:可可西里 更新时间:2023-11-01 07:06:50 29 4
gpt4 key购买 nike

当我使用 codeigniter php 运行 csv 导入时,我遇到了这个 mysql 错误

enter image description here

Reason for this error is that unwanted characters or symbols in the string.

这是我使用的 CodeIgniter 代码。

$this->db->where('designationName', $row[$ex_start + 3]);
$q = $this->db->get('designation');
if ($q->result()) {
$id = $q->result_array();
$designation = $id[0]['iddesignation'];

} else {
$data = array(
'designationName' => $row[$ex_start + 3], //this is the variable that get string
'designationDate' => date('Y-m-d'),
'status_idstatus' => '1'
);
$this->db->insert('designation', $data); //this is the point that error occurs
$designation = $this->db->insert_id();

}

我如何避免这种字符或符号?

最佳答案

你可以做到

$data = array(
'designationName' => preg_replace('/[^a-zA-Z0-9-_\.]/','', $row[$ex_start + 3])
'designationDate' => date('Y-m-d'),
'status_idstatus' => '1'
);

Note: This will allow plain text+numbers to save. No, any special chars will pass


编辑 01

如果你需要允许一些特殊字符(所有键盘字符)

 preg_replace('/[^A-Za-z0-9_~`\/@!$.%^#&*\\()+-=]/','', $row[$ex_start + 3])

或者您可以使用 Escaping Queries in codeigniter

  1. $this->db->e​​scape()
  2. $this->db->e​​scape_str()
  3. $this->db->e​​scape_like_str()

关于php - 在 codeigniter 中如何删除字符串中不需要的字符或符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48920094/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com