Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

enter image description here

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

This is the CodeIgniter code I use.

$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 ?

like image 381
Supun Abesekara Avatar asked Oct 16 '22 22:10

Supun Abesekara


1 Answers

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

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

  1. $this->db->escape()
  2. $this->db->escape_str()
  3. $this->db->escape_like_str()
like image 85
Abdulla Nilam Avatar answered Oct 21 '22 00:10

Abdulla Nilam