加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

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

发布时间:2020-12-11 23:50:08 所属栏目:MySql教程 来源:网络整理
导读:当我使用codeigniter php运行csv import时,我坚持使用这个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-ge

当我使用codeigniter php运行csv import时,我坚持使用这个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

> $this-> db-> escape()
> $this-> db-> escape_str()
> $this-> db-> escape_like_str()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读