In codeigniter how to remove unwanted characters or characters in a string

When I start csv import using codeigniter php, I got stuck in this mysql error

enter image description here

The cause of this error is unwanted characters or characters in a string.

This is the CodeIgniter code I'm using.

$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('Ym-d'), 'status_idstatus' => '1' ); $this->db->insert('designation', $data); //this is the point that error occurs $designation = $this->db->insert_id(); } 

How do I avoid such characters or characters?

+5
source share
1 answer

You can do

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

Note. . This will save plain text + numbers. No, any special characters will pass


Change 01

If you need to allow some special characters (all keyboard characters)

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

Or you can use query execution in codeigniter

  • $this->db->escape()
  • $this->db->escape_str()
  • $this->db->escape_like_str()
+3
source

Source: https://habr.com/ru/post/1275497/


All Articles