<?php function test_name($name) { if (1 === preg_match("/^[A-Za-z][A-Za-z0-9]*(?:[_-][A-Za-z0-9]+)*\$/", $name)) { print "{$name} id OK\n"; } else { print "{$name} failed\n"; } } test_name("a_name"); test_name("a_name_more_and_more"); test_name("_a_name"); test_name("a-name-more-and-more"); test_name("-a_name"); test_name("a-name-more_and-more_some");
function test_sort($a, $b) { global $cwd; $a = test_name($a); $b = test_name($b); $ta = strpos($a, "{$cwd}/tests") === 0 ? 1 + (strpos($a, "{$cwd}/tests/run-test") === 0 ? 1 : 0) : 0; $tb = strpos($b, "{$cwd}/tests") === 0 ? 1 + (strpos($b, "{$cwd}/tests/run-test") === 0 ? 1 : 0) : 0; if ($ta == $tb) { return strcmp($a, $b); } else { return $tb - $ta; } }
</script> <?php } else { $is_examid = true; } if (empty($name)) { $nameErr = "名字未填写"; ?> <script> alert("<?php echo $nameErr; ?> "); </script> <?php } elseif (!test_name($name)) { $nameErr = "姓名为2~3个汉字"; ?> <script> alert("<?php echo $nameErr; ?> "); </script> <?php } else { $is_name = true; } if ($is_examid && $is_name) { $_SESSION['examid'] = $examid; $_SESSION['name'] = $name;