简体   繁体   English

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

[英]In codeigniter how to remove unwanted characters or symbols in string

When i run csv import using codeigniter php i stuck with this mysql error 当我使用codeigniter php运行csv import时,我坚持使用这个mysql错误

在此输入图像描述

Reason for this error is that unwanted characters or symbols in the string. 此错误的原因是字符串中不需要的字符或符号。

This is the CodeIgniter code I use. 这是我使用的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();

}

How I avoid this kind of characters or symbols ? 我如何避免这种字符或符号?

you can do 你可以做

$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 不,任何特殊的字符都会通过


Edit 01 编辑01

If you need to allow some special chars (all keyboard chars) 如果你需要允许一些特殊的字符(所有键盘字符)

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

Or you can use Escaping Queries in codeigniter 或者您可以在codeigniter中使用Escaping Queries

  1. $this->db->escape()
  2. $this->db->escape_str()
  3. $this->db->escape_like_str()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM