我使用Code Igniter中的数组将表单内容提交到我的数据库。这是相关的代码......
$sql= array (
'currency'=>$this->input->post('currency'),
'total'=>$this->input->post('total'),
'expenses'=>$this->input->post('expenses'),
);
$ins = $this->db->insert('donations',$sql);
如果在写入数据库时,如何在货币字段中转义£符号?我知道我需要使用str_replace()
£
,但无法理解PHP。
答案 0 :(得分:2)
您正在寻找的功能是htmlentities
$sql = array (
'currency' => htmlentities($this->input->post('currency'), ENT_QUOTES, "UTF-8"),
'total' => $this->input->post('total'),
'expenses' => $this->input->post('expenses'),
);
$ins = $this->db->insert('donations',$sql);
答案 1 :(得分:0)
你最好只允许值进入数据库,例如£10.00
然后使用:
echo htmlentities($value);
当你从数据库中取回它并写入html doc
时答案 2 :(得分:0)
停止。
以尽可能简单的形式将数据输入数据库,并在显示时对显示进行排序。
更重要的是,你无法区分英镑英镑和英镑意味着GIP。
您需要在插入之前将输入转换为相关值。
一个解决方案(遵循harryg建议使用ISO代码)是这样的:
// pass the input to a variable
$currency_raw = $this->input->post('currency');
// potentially, process it to standardise (trim, uppercase etc)
// use a switch to set the $currency value you'll insert
switch ($currency_raw) {
case "£":
$currency = "GBP";
break;
default:
// unrecognised currencies should error
$currency = "???";
}
$sql= array (
'currency'=>$currency,
'total'=>$this->input->post('total'),
'expenses'=>$this->input->post('expenses'),
);
$ins = $this->db->insert('donations',$sql);
您可以做类似的事情,将GBP转换回£。
在大型应用程序中,您可以通过将货币存储为int并具有查找表来获得速度和数据大小的好处。
但是,您可能需要真正查看表单字段。您可以在下拉列表中显示£(或更好,“£(GBP)”,但提交的值为GBP,这将是最好的。