[英]How to know which variable has a value and which one is null?
I need to validate an input which I will need to later check against a database for its presence in the table. 我需要验证一个输入,我需要稍后检查数据库是否存在于表中。 The input either has to be a mobile number (all digits, 10 max len) or an email address.
输入必须是手机号码(所有数字,10最大len)或电子邮件地址。 This below is what I did
以下是我的所作所为
<?php
$credential = '9999999999';
if (ctype_digit($credential)) {
$mobile_number = $credential;
echo 'Mobile: '. $mobile_number;
} elseif (filter_var($credential, FILTER_VALIDATE_EMAIL)) {
$email = $credential;
echo 'Email: '.$email;
}
output when $credential = '9999999999'
; 当
$credential = '9999999999'
时输出 ;
Mobile: 9999999999
output when $credential = 'abc@gmail.com'
; 当
$credential = 'abc@gmail.com'
时输出 ;
Email: abc@gmail.com
Two questions, 两个问题,
Is there a better way of doing it? 有没有更好的方法呢? Like, using preg_match.
比如,使用preg_match。
The above code manages to differentiate between an email and a mobile number which later I need to match against a database. 上面的代码设法区分电子邮件和手机号码,以后我需要与数据库进行匹配。 In a real scenario, the input will be user-supplied,
$credential = $_POST['credential'];
在实际场景中,输入将由用户提供,
$credential = $_POST['credential'];
and then either of two variable will be set accordingly. 然后相应地设置两个变量中的任何一个。 How do I know which of the two variable is set?
我如何知道设置了哪两个变量? If I am to match email to email column or mobile number to the mobile column, I need to know which of the two has value and which one is null.
如果我要将电子邮件与电子邮件列或移动电话号码匹配到移动列,我需要知道两者中哪一个有值,哪一个为空。
filter_var($credential, FILTER_VALIDATE_EMAIL)
is going to perform better than regex, because that is what it is specifically designed to do. filter_var($credential, FILTER_VALIDATE_EMAIL)
性能优于正则表达式,因为它是专门设计的。
As for the phone numbers, this seems a little undercooked or too strict. 至于电话号码,这似乎有点未煮熟或太严格。 Many people add spaces, parentheses, hyphens, etc without much thought.
许多人没有多想就添加空格,括号,连字符等。 It will only be irritating to be so strict on the phone number, I think.
我认为,对电话号码如此严格只会令人恼火。 There are plenty of regex patterns on SO regarding phone number validation.
SO上有很多关于电话号码验证的正则表达式模式。 Intimate knowledge of your expected phone number strings are required to answer this confidently.
对您预期的电话号码字符串的深入了解需要自信地回答这个问题。 Are you expecting US numbers only?
你只期待美国的数字吗? is this international?
这是国际化的? We don't know, so I can't say what is the best strategy.
我们不知道,所以我不能说什么是最好的策略。
This will work: 这将有效:
if (ctype_digit($credential) && strlen($credential)==10) {
$mobile_number = $credential;
echo 'Mobile: '. $mobile_number;
} elseif (filter_var($credential, FILTER_VALIDATE_EMAIL)) {
$email = $credential;
echo 'Email: '.$email;
}
This will also work: 这也有效:
if (preg_match('/^\d{10}$/',$credential)) {
$mobile_number = $credential;
echo 'Mobile: '. $mobile_number;
} elseif (filter_var($credential, FILTER_VALIDATE_EMAIL)) {
$email = $credential;
echo 'Email: '.$email;
}
The speed differences between the two methods will be totally unnoticeable to your users. 两种方法之间的速度差异对用户来说完全没有意义。 You should choose whichever approach you prefer personally.
你应该选择你个人喜欢的方法。
Thank you for answering my first question. 感谢您回答我的第一个问题。 For the second part, if you interested, I found this http://php.net/manual/en/function.is-null.php (untested).
对于第二部分,如果您感兴趣,我发现了这个http://php.net/manual/en/function.is-null.php (未经测试)。
This is how you can do it with PHP 这就是你用PHP做的方法
if (preg_match('/^\d{10}$/', $string) || filter_var($string, FILTER_VALIDATE_EMAIL)) {
//valid
} else {
//invalid
}
however, you should validate this on client-side as well, to make sure a request is not even sent to the server if the value is invalid. 但是,您应该在客户端验证这一点,以确保在值无效时甚至不将请求发送到服务器。
EDIT: 编辑:
if (preg_match('/^\d{10}$/', $string)) {
//valid mobile
} else if (filter_var($string, FILTER_VALIDATE_EMAIL)) {
//valid email
} else {
//invalid
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.