在 codeigniter 中如何删除字符串中不需要的字符或符号
In codeigniter how to remove unwanted characters or symbols in string
当我 运行 使用 codeigniter php 导入 csv 时,我遇到了这个 mysql 错误
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
$this->db->escape()
$this->db->escape_str()
$this->db->escape_like_str()
当我 运行 使用 codeigniter php 导入 csv 时,我遇到了这个 mysql 错误
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
$this->db->escape()
$this->db->escape_str()
$this->db->escape_like_str()