You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP中通过6位字母数字验证码匹配并显示学生信息?

PHP Student Verification System Implementation

Here's a practical, easy-to-follow solution that meets your requirements. I'll walk through the approach first, then share the full working code with explanations.

Core Approach

  • Step 1: Store student data in a structured array with all necessary details (name, short course code, grade, student ID).
  • Step 2: Create a mapping for short course codes to their full names (e.g., bc → BCA).
  • Step 3: Validate the input code format (6 characters total: 2 letters + 1 digit + 3 digits) and parse out the course code, grade, and student ID.
  • Step 4: Match the parsed values against the student records and display the full details if a match is found.

Complete Working Code

<?php
// Student database - add more entries as needed
$students = [
    [
        'name' => 'John Doe',
        'course_code' => 'bc',
        'grade' => '1',
        'student_id' => '001'
    ],
    [
        'name' => 'Jane Smith',
        'course_code' => 'bs',
        'grade' => '2',
        'student_id' => '005'
    ],
    [
        'name' => 'Raj Patel',
        'course_code' => 'mc',
        'grade' => '1',
        'student_id' => '010'
    ]
];

// Map short course codes to their full names
$course_name_map = [
    'bc' => 'BCA',
    'bs' => 'BSC(it)',
    'mc' => 'MCA'
];

// Function to validate input and fetch student details
function retrieveStudentInfo($input_code, $student_list, $course_map) {
    // Check if input is exactly 6 characters and follows the correct pattern
    if (strlen($input_code) !== 6 || !preg_match('/^[a-z]{2}[0-9]{4}$/i', $input_code)) {
        return "Error: Invalid code format! Please enter a 6-character code (2 letters + 4 digits).";
    }

    // Parse the input code into components
    $short_course = strtolower(substr($input_code, 0, 2));
    $grade = substr($input_code, 2, 1);
    $student_id = substr($input_code, 3, 3);

    // Validate course code exists in our mapping
    if (!isset($course_map[$short_course])) {
        return "Error: Unknown course code!";
    }

    // Search for matching student record
    foreach ($student_list as $student) {
        if ($student['course_code'] === $short_course && 
            $student['grade'] === $grade && 
            $student['student_id'] === $student_id) {
            
            // Format the result for display
            $full_course_name = $course_map[$short_course];
            return "✅ Student Found!\n" .
                   "Name: {$student['name']}\n" .
                   "Course: {$full_course_name}\n" .
                   "Grade: {$student['grade']}\n" .
                   "Student ID: {$student['student_id']}";
        }
    }

    // No matching record found
    return "❌ No student matches the provided code.";
}

// Example usage (replace with user input in a real scenario)
$user_input = 'bc1001';
echo retrieveStudentInfo($user_input, $students, $course_name_map);
?>

Key Features Explained

  • Validation: The function first checks if the input code meets the required length and pattern to avoid invalid entries.
  • Case Insensitivity: Converts the course code to lowercase so inputs like BC1001 work just like bc1001.
  • Clear Feedback: Provides specific error messages for invalid formats, unknown course codes, or missing records.
  • Scalability: Adding new students or course codes is as simple as updating the respective arrays.

Test the Example

If you run the code with $user_input = 'bc1001', you'll get this output:

✅ Student Found!
Name: John Doe
Course: BCA
Grade: 1
Student ID: 001

内容的提问来源于stack exchange,提问作者Deepak Dhan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 16:27:35