入力フィールド内の文字列長の条件を検出しようとしています。ifステートメントからどの条件が満たされているかを知り、それを配列に入れて表示する必要があります。これが私のコードです:
if(strlen($post['first_name']) > 25
|| strlen($post['last_name']) > 25
|| strlen($post['email']) > 40
|| strlen($post['phone']) > 15
){
$lengthTooLong = array();
//do this
}
foreachを使用できます。
// define which keys of $post you want to check
// array value defines max length of value from $post[$key]
$keyToMaxLength = array(
'first_name' => 25,
'last_name' => 25,
'email' => 40,
'phone' => 15,
);
$lengthTooLong = array();
foreach($keyToMaxLength as $key => $maxLength) {
if(strlen($post[$key]) > $maxLength) {
$lengthTooLong[] = $key; // or $post[$key]
}
}
echo var_export($lengthTooLong, true);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加