2017-03-06 10 views
0

リスト内の特定のデータ(15桁など)のフォームフィールドを検証したいと思います。下のコード例では、番号のリストをスキャンするコードのヘルプが必要です。以下の2つのエントリが例ですが、実際のリストは数百行になります。リストに対するフォームフィールドの検証

public function validate($value) 
{ 
    $file_location = '/path/to/file/a1.txt'; 

    // Mage::log($file_location, null , 'aa.log', 1); 

    $allowed_values = explode("\n",trim(file_get_contents($file_location))); 
    array_walk($allowed_values, 'trim'); 
    // $allowed_values = array(''); 

    return in_array($value, $allowed_values); 
} 

リストの例

C810,34402,BB-H500,356523076903149,TESTE 
C810,34402,BB-H500,356523076452163,TESTE 

+0

だから...このコードは動作していますか、動作していませんか? \ n ""を 'PHP_EOL'に変更することも賢明かもしれません。 – Xorifelse

答えて

0

これはCSV形式ですありがとうございますか!次に試してみてください

public function validate($value) 
{ 
    $file_location = '/path/to/file/a1.txt'; 

    $csv = array_map('str_getcsv', file($file_location)); //Get all values from the file 
    $allowed_values = array_column(3); // Replace 3 by the index of the field 
    return in_array($value, $allowed_values); 
} 
+0

ありがとう@Exalyonファイルは.csvファイルではなく.txtファイルです。 – Elvis

関連する問題