2012-09-07 28 views
1

直接從CodeIgniter page拍攝。PHP中的分隔(|)?

$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' ...)); 

是不是容易得多處理數組,而不是浪費不必要的代碼檢查|符號和獨立標籤?

+0

請給出一個實際上導致文本「_directly從CodeIgniter page_」的鏈接。這可能會給其他人一些見解。 – Tadeck

+3

「處理數組不是很容易,而不是浪費不必要的代碼來檢查符號和分離標記嗎?」 --- codeigniter不是你想要學習的一款軟件。所以不要懷疑你的代碼中是否有奇怪的東西;-) – zerkms

+0

嗨,在這裏:http://codeigniter.com/user_guide/libraries/form_validation.html – Aristona

回答

3

CodeIgniter將在該字符串上運行explode(),使用|(管道)作爲分隔符。所有這一切都是設計師的喜好和創造力。

下面是CI source的片段,它分裂。

// 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']); 
    } 
+0

爆炸可用PHP – FirmView

+0

因此,explode()函數將工作,只要你想清理變量。這不是沒有必要嗎? explode()會被調用幾次,導致性能下降,維護這樣的代碼也非常困難,就像你忘記放一個| (比如requiredxss_clean),它不會清除xss或使該字段成爲必需字段,甚至可能導致數據庫端出現問題。我並不想捆綁CodeIgniter,之前沒有真正使用它,只是沒有看到|的用法在PHP之前很想問。 – Aristona

+0

所有'explode();'將要做的是將一個字符串轉換爲一個數組,並使用指定的字符作爲分隔符。從您的角度來看,這是沒有必要的,但FW開發人員希望爲其框架的用戶提供便利。像這樣的小事讓一個人比另一個人更愉快。 –