ここに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;
}
?>
「case」ステートメントの数字を引用すると、それらは文字列です。 – neuhaus
代わりに 'case '02':'などの文字列を使用します。 – BenM
私はそれらを削除しましたが、以前は引用符で囲まれていましたが、まだそのようなものはありません –