2017-08-30 21 views
0

ここに2つの問題があります。最初に、数字と特殊文字を除外するのにpreg_matchを使用する方法はわかりません。文字と&を含めることを許可する必要があります。Switchのステートメントでは、必要なケースが出てこない

また、このスクリプトは動作しません。私はそれが動作することを意味しますが、switchステートメントは、エラーが含まれている最後の文字列を配信し、foreachの中に入れた場合、最初のエラーは1回、2番目のエラーは3回発生します。

私は間違っていますか?私を助けてください!

<?php 
// test variables 
$act1 = "SUBSCRIBEa"; 
$act2 = "SUBSCRIBEb"; 
$act3 = "SUBSCRIBE"; 
$act4 = "SUBSCRIBE"; 

// set error false as default 
$error = "false"; 

// check if variables are ready for use 
if(!empty($act1) && !empty($act2) && !empty($act3) && !empty($act4)) { 
$acts = [$act1, $act2, $act3, $act4]; 

// check the acts for lenght, numbers and special characters 
// add all of the acts to an array to loop over 
foreach($acts as $key => $value) { 

    if($key < 9) { 
     $errorKey = "0{$key}"; 
    } else { 
     $errorKey = $key; 
    } 

    // check the lenght 
    if(strlen($value) > 15) { 
     $error = "true"; 
     $errorNumber = $errorKey; 
    } 

    /* check for numbers and special characters 
    if(!preg_match('/[^a-z&A-Z]/', $value)){ 
     $error = "true"; 
     $errorNumber = $error_{$errorKey}; 
    } 
*/ 

// declare a whitelist of things that should not produce an error 
$whiteList = [ 
    'SUBSCRIBE', 
    'SUB & LIKE', 
    'LIKE & COMMENT', 
    'DISLIKE', 
    'COMMENT', 
    'LIKE', 
    'FOLLOW', 
]; 

    // check if value from act is in the whitelist declared above, if its not, set `$error` to true and set `$error_*` (with key) to "true" as well. 
    if(!in_array($value, $whiteList)) { 
     $error = "true"; 
     $errorNumber = $errorKey; 
    } 

} 
} 
// deliver the error message 
switch($errorNumber){ 
    case 00: 
     echo "Something went wrong here 1 :o"; 
     break; 
    case 01:  
     echo "Something went wrong here 2 :o"; 
     break; 
    case 02: 
     echo "Something went wrong here 3 :o"; 
     break; 
    case 03: 
     echo "Something went wrong here 4 :o"; 
     break; 
} 
?> 
+1

「case」ステートメントの数字を引用すると、それらは文字列です。 – neuhaus

+0

代わりに 'case '02':'などの文字列を使用します。 – BenM

+0

私はそれらを削除しましたが、以前は引用符で囲まれていましたが、まだそのようなものはありません –

答えて

0

次のコードを試してみて、私があなたの問題を解決した可能性があることを示すようにコメントしました。

<?php 
    // test variables 
    $act1 = "SUBSCRIBEa"; 
    $act2 = "SUBSCRIBEb"; 
    $act3 = "SUBSCRIBE"; 
    $act4 = "SUBSCRIBE"; 

    // set error false as default 
    $error = false; 

    // check if variables are ready for use, if they are, add them to `$acts` array 
    // I do each check as a seperate line, as it looks cleaner than 1 long if statement. 
    $acts = []; 
    if(!empty($act1)) $acts[] = $act1; 
    if(!empty($act2)) $acts[] = $act2; 
    if(!empty($act3)) $acts[] = $act3; 
    if(!empty($act4)) $acts[] = $act4; 

    // declare a whitelist (outside of loop) of things that should not produce an error 
    $whiteList = [ 
     'SUBSCRIBE', 
     'SUB & LIKE', 
     'LIKE & COMMENT', 
     'DISLIKE', 
     'COMMENT', 
     'LIKE', 
     'FOLLOW', 
    ]; 

    //create an empty array to hold errors 
    $errors = []; 

    // check the acts for lenght, numbers and special characters 
    // add all of the acts to an array to loop over 
    foreach($acts as $key => $value) { 

     //1 line ternary is cleaner than if/else statetmnt 
     $errorKey = $key < 9? "0{$key}" : $key; 

     //each row by default has no error 
     $hasError = 0; 

     // check the lenght 
     //use regex to check if a string only contains letters. 
     // check if value from act is in the whitelist declared above, if its not, set `$error` to true and set `$error_*` (with key) to "true" as well. 
     // make sure you use `||` (or) here, not `&&` (and), otherwise this statement will not work properly. If it's `||` only 1 thing has to be wrong, if it's `&&` then all of them have to be wrong. 
     if(strlen($value) > 15 || preg_match('/[^A-Za-z &]/', $value) || !in_array($value, $whiteList)) { 
      $error = true; 

      //if error occurs, set `$hasError` to 1, to later insert errorKey into array. 
      $hasError = 1; 
     } 

     // 
     if($hasError) { 
      //store error in array, to loop through later 
      $errors[] = $errorKey; 
     } 

    } 

    // deliver the error message 
    //Check if $error has been set to true at any point 
    if($error) { 
     //loop through error array, echo error message if $errorNumber matches. 
     //at this point we KNOW there was an error at some point, no need to use a switch really 
     foreach($errors as $errorNumber) { 
      echo "Something went wrong here $errorNumber :o"; 
     } 
    } 
?> 
+0

それは私に今何も表示されません:O、エラーメッセージなし –

+0

まあ、あなたのテストケースのうち、 – GrumpyCrouton

+0

ホワイトリストにエラーがあるはずです –

関連する問題