我使用的是带有cakehp2.0的FormHelper。在保存到数据库之前,我想从输入字段中提取一个21位数字,并且只将该数字存储在我的数据库中。
我目前拥有的代码是:
if (!empty ( $this->request->data ) && ($this->request->is('post')))
{
preg_match("/[0-9]{21}/", $this->request->data, $field_id);
$this->InsightId->save($field_id[0]);
}
if语句从不进行求值,也从不进入preg_match。我做错了什么?
我的解决方案采用了与Regex完全不同的路线。我使用php以"/"作为分隔符。我的代码现在看起来是这样的:
if (!empty ( $this->request->data ) && ($this->request->is('post')))
{
$field_ids = explode('/', $this->request->data['InsightId']['field']);
foreach($field_ids as $field_id)
{
if(strlen($field_id) === 21)
{
$this->request->data['InsightId']['field'] = $field_id;
$this->InsightId->save($this->request->data);
}
}
}