【发布时间】:2017-03-18 02:18:41
【问题描述】:
在使用 php 验证正则表达式时遇到问题。
我在下面发布了一个 html 表单,要求提供电话号码、车牌、街道地址、生日和社会保险号码。 (我现在只关心让电话号码和街道正常工作)
我需要使用 preg_match 函数来遵守以下电话号码标准:
电话号码 - 7 位或 10 位
◦7 位数字:前三位数字为一组,可以与后四位数字用破折号、一个或多个空格或什么都没有分隔
◦10 位数字:前三位数字、后三位数字和最后四位数字是三个不同的组,每个组与相邻组之间可以用空、破折号、一个或多个空格或括号分隔
◦例如。所有这些都是有效的 ▪(604)123-4567 但不是 604)123-4567 而不是 (604123-4567 ▪6041234567 ▪1234567 ▪123-4567 ▪123 4567 ▪604-123-4567 ▪604 123 4567 ▪604 1234567 ▪604123 456
我需要使用 preg_match 函数来遵守以下电话号码标准:
街道地址 – 三到五个数字地址后跟一个字符串,必须以单词“街道”结尾 ◦例如。这些是有效的 ▪123大街 ▪橡树街8888号 ▪55555 邓斯缪尔街
lab11.html 和 lab11.php 的代码 lab11.html
<!DOCTYPE html>
<html>
<head>
<title>Lab 11</title>
<meta charset="utf-8">
</head>
<body>
<form action="lab11.php" method="POST">
<input type="text" name="phoneNumber"placeholder="Phone Number" style="font-size: 15pt">
<br>
<input type="text"name="licensePlate"placeholder="License Plate" style="font-size: 15pt">
<br>
<input type="text" name="streetAddress" placeholder="Street Address" style="font-size: 15pt">
<br>
<input type="text" name="birthday" placeholder="Birthday" style="font-size: 15pt">
<br>
<input type="text" name="socialInsuranceNumber" placeholder="Social Insurance Number" style="font-size: 15pt">
<br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
lab11.php
<?php
// Get phone number, license plate, street address, birthday and
// social insurance number entered from lab11.html form
$phoneNumber = $_POST['phoneNumber'];
echo "Your phone number is " . $phoneNumber;
echo "<br>";
$licensePlate = $_POST['licensePlate'];
echo "Your License Plate Number is " . $licensePlate;
echo "<br>";
$streetAddress = $_POST['streetAddress'];
echo "Your Street Address is " . $streetAddress;
echo "<br>";
$birthday = $_POST['birthday'];
echo "Your Birthday is " . $birthday;
echo "<br>";
$socialInsuranceNumber = $_POST['socialInsuranceNumber'];
echo "Your Social Insurance Number is " . $socialInsuranceNumber;
echo "<br>";
// Validate regular expression for phone number entered
if (preg_match("/^\(.[0-9]{3}[0-9]$/", $phoneNumber)) {
echo "Your phone is correct.";
}
else {
echo "Your password is wrong.";
}
// Validate regular expression for license plate entered
if (preg_match("/{3,5}.String$/", $streetAddress)) {
echo "Your plate is correct.";
}
else {
echo "Your plate is wrong.";
}
?>
【问题讨论】:
标签: php preg-match