2017-08-30 39 views
0

我在这里有两个问题。首先,我不知道如何使用preg_match来过滤数字和特殊字符,只允许包含字母和&。开关语句没有给出我想要的情况

此外,该脚本不能如何应用。我的意思是它的工作原理,但switch语句只提供最后一个包含错误的字符串,如果我把它放在foreach里面,它会给出第一个错误1次和第二个错误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

白名单应该错误 –