2013-08-23 104 views
0

基本上,我有一個通過foreach循環從多維數組構建的變量。如何驗證字符串的格式?

我需要以某種方式驗證它所在的格式,雖然我不確定如何去解決這個問題,因爲在PHP中我仍然是一個新手。

這是我的字符串的樣子:

question1,answer1,answer2,answer3,answer4 
question2,answer1,answer2,answer3,answer4 
question3,answer1,answer2,answer3,answer4 

我需要驗證正確的格式,是:

string[comma]string[comma]string[comma]string[comma]string 
string[comma]string[comma]string[comma]string[comma]string 
string[comma]string[comma]string[comma]string[comma]string 

我需要一個布爾變量如果可能的話,以驗證它,返回如果它與格式匹配,則爲true或false。這些字符串可以包含任何內容,如字母,數字,特殊字符等。

每行將始終有5個字符串和4個逗號,從不多也不少。

如果需要,這是構建多維數組然後將其轉換爲字符串的代碼。它抓取上傳的CSV文件,將其轉換爲多維數組,然後再構建字符串。正如你所看到的,我的數組從1開始,而不是從0開始,沒有解釋爲什麼這是脫離主題的原因。

$csv_array = array(array()); 
    if (!empty($_FILES['upload_csv']['tmp_name'])) 
    { 
     $file = fopen($_FILES['upload_csv']['tmp_name'], 'r'); 
    } 

    if($file) 
    { 
     while (($line = fgetcsv($file)) !== FALSE) 
     { 
      $csv_array[] = array_combine(range(1, count($line)), array_values($line)); 
     } 

     fclose($file); 
    } 


    if(!empty($csv_array[1])) 
    { 
     foreach (array_slice($csv_array,1) as $row) 
     { 
      $all_questions = $all_questions . $row[1] . ","; 
      $all_questions = $all_questions . $row[2] . ","; 
      $all_questions = $all_questions . $row[3] . ","; 
      $all_questions = $all_questions . $row[4] . ","; 
      $all_questions = $all_questions . $row[5]; 
      $all_questions = $all_questions . "\n"; 
     } 
    } 

非常感謝所有幫助。

在此先感謝!

+3

使用'爆炸()',並計算所產生的陣列多少項目了?但是,如果沒有一個字符串裏面有逗號,這隻會起作用。 – andrewsi

+0

'如果(計數($線)== 5)' –

+0

@andrewsi OP已經使用'fgetcsv()',所以他不會有問題。 –

回答

1

聽起來像一個正則表達式可以做的工作:如果你想允許[string]是空的,你可以改變所有+*

$match_ok = preg_match('/^([^,]+,){4}[^,]+$/', $test_string); 

說明:第一個^匹配行開頭,然後一個或多個不是逗號的字符,後面跟着一個逗號。這個序列必須在那裏4次,並且必須緊接着另一個沒有以逗號結尾的字符串。 $匹配行的末尾。

如果你想匹配,而多行字符串,你可以使用:

$match_ok = preg_match('/^(([^,]+,){4}[^,]+\n){3}$/', $test_string); 

(因爲多模式preg_match作品被默認)

編輯2:你甚至可以使正則表達式匹配字符串不是由單獨處理的最後一行換行結束,只是因爲它是用做的cols:

$match_ok = preg_match('/^(([^,]+,){4}[^,]+\n){2}([^,]+,){4}[^,]+$/', $test_string); 
+0

這會失敗,因爲我的字符串有換行符,就像'$ test_string =「question1,answer1,answer2,answer3,answer4 \ nquestion2,answer1,answer2,answer3,answer4 \ nquestion3,answer1,answer2,answer3,answer4」;' – Fizzix

+0

當我用你的正則表達式運行這個時,它回聲'壞'。任何想法爲什麼? ? '' – Fizzix

+0

對不起,暫時字符串以一個新行結束?正則表達式匹配以換行符結尾的字符串 – urzeit

-1

正則表達式將會幫助你:

$test_string = "Some,string,you,want,to,test"; 
if(preg_match('@^([a-zA-Z0-9]+\,)+([a-zA-Z0-9])[email protected]', $test_string)) { 
    echo 'All ok'; 
} 
+0

這是如何確定有五個字符串和四個逗號的? –

0

試試這個簡單的一個,這個

<?php 
$test_string = "question1,answer1,answer2,answer3,answer4"; 
$newarray=explode(',',$test_string); 
if(count($newarray)=='5'){ 
    your code......; 
    } 
?> 

有可能通過這麼多其他的解決方案---------- ---------- TRUE FALSE -------------

<?php 
    $test_string = "questionAsdAD1###,234234,answer2,answer3,answer4"; 
    function ToCheckMyStrung($test_string){ 
    $newarray=explode(',',$test_string); 
    if(count($newarray)=='5'){ 
     return true; 
    } else { 
     return false; 
    } 
    } 
    ?> 
+0

偉大的思想。雖然,它不測試每個問題中的換行符?例如,有5個字符串和4個逗號,然後換行,然後重複序列。 – Fizzix

+0

這將失敗,因爲我的字符串可以有換行符,像這樣'$ test_string =「問題1,ANSWER1,ANSWER2,ANSWER3,answer4 \ nquestion2,ANSWER1,ANSWER2,ANSWER3,一個swer4 \ nquestion3,ANSWER1,ANSWER2,ANSWER3,answer4 「;' – Fizzix

+0

另外,如果我用'echo'替換'return',則不會顯示任何內容。我相信你的代碼中有一個錯誤。 – Fizzix

0

中的foreach 使用

implode(',',$row); for proper formatting. 
0

你可以做到這一點,而你正在創建的字符串變量,

$count=1; 
$trace=array(); 
if(!empty($csv_array[1])) 
{ 
    foreach (array_slice($csv_array,1) as $row) 
    { 
     $all_questions = $all_questions . $row[1] . ","; 
     $all_questions = $all_questions . $row[2] . ","; 
     $all_questions = $all_questions . $row[3] . ","; 
     $all_questions = $all_questions . $row[4] . ","; 
     $all_questions = $all_questions . $row[5]; 
     $all_questions = $all_questions . "\n"; 
     if($row[1]=="" || $row[2]=="" || $row[3]=="" || $row[4]=="" || $row[5]=="") 
     { 
       $trace[]=$count; 
     } 
     $count++; 
    } 
} 

,並不僅僅是使用$跟蹤陣列的,只要你想。