PHP中的分隔(|)


Separation (|) in PHP?

直接从CodeIgniter页面获取。

$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|xss_clean');

本部分:

'trim|required|min_length[5]|max_length[12]|xss_clean'

PHP是否有一个通过|检查来分隔值的内置功能,或者CodeIgniter手动进行?

如果是,

他们为什么不用这样的东西?

set_rules('username', 'Username', array('trim', 'required' ...));

处理数组不是比浪费不必要的代码来检查符号和分离标记容易得多吗?

CodeIgniter将在该字符串上运行explode(),使用|(管道)作为分隔符。一天下来,所有的都是设计师的喜好和创造力。

以下是执行拆分的CI源中的片段。

// Cycle through the rules for each field, match the
// corresponding $_POST item and test for errors
    foreach ($this->_field_data as $field => $row)
    {
        // Fetch the data from the corresponding $_POST or validation array and cache it in the _field_data array.
        // Depending on whether the field name is an array or a string will determine where we get it from.
        if ($row['is_array'] === TRUE)
        {
            $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
        }
        elseif (isset($validation_array[$field]) && $validation_array[$field] !== '')
        {
            $this->_field_data[$field]['postdata'] = $validation_array[$field];
        }
        // Don't try to validate if we have no rules set
        if (empty($row['rules']))
        {
            continue;
        }
        $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
    }