我可以单独询问这三个,但决定将它们合并。
我想通过以下示例询问一些专家意见:
如何正确验证字母数字字符串? (只有拉丁字母和数字)
如何正确验证书面unicode字符串? (如上所述,但允许任何国家/地区字母)
如何正确验证字符串是否像电子邮件一样?我猜测最好的是filter_var($string,FILTER_VALIDATE_EMAIL)
(我猜它与url和ip相同)
谢谢。
答案 0 :(得分:2)
对于#1,请使用ctype_alnum()
。它比正则表达式更快,你不必担心你是否正确使用正则表达式。我也认为它更整洁。
答案 1 :(得分:0)
preg_match('/[a-zA-Z0-9]+/', $str)
答案 2 :(得分:0)
filter_very非常有效且特殊用途,但也有限。
你也只得到一个过滤后的返回字符串,你必须将其与原始字符串进行比较,看它是否适合。
在允许的字符旁边可能存在某些要求和/或结构,您无法以这种方式检查。
最常用的方法是使用pcre函数,尤其是preg_match。 它非常有效,你可以直接使用返回值。
你有正则表达式的全部可能性。 例如,您希望验证每个存在的名称是否为exacmt形式“Mr / Mrs Firstname Lastname,akademic-title”。
当它变得棘手时,如果你只想允许某些范围的unicode字符。
例如,如果您只想允许U + 0600-U + 06FF(1536-1791)(阿拉伯语)。加上一定范围的dingbats和括号或其他东西。
没有预定义的字符类,并且定义它们不会那么优雅。
在这种情况下,最好的方法是逐个循环遍历文本并检查范围......
答案 3 :(得分:0)
这是一个适用于电子邮件验证的方法。
以下是电子邮件地址的要求,并附有相关参考:
/**
Validate an email address.
Provide email address (raw input)
Returns true if the email address has the email
address format and the domain exists.
*/
function validEmail($email)
{
$isValid = true;
$atIndex = strrpos($email, "@");
if (is_bool($atIndex) && !$atIndex)
{
$isValid = false;
}
else
{
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64)
{
// local part length exceeded
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255)
{
// domain part length exceeded
$isValid = false;
}
else if ($local[0] == '.' || $local[$localLen-1] == '.')
{
// local part starts or ends with '.'
$isValid = false;
}
else if (preg_match('/\\.\\./', $local))
{
// local part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
{
// character not valid in domain part
$isValid = false;
}
else if (preg_match('/\\.\\./', $domain))
{
// domain part has two consecutive dots
$isValid = false;
}
else if
(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
str_replace("\\\\","",$local)))
{
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/',
str_replace("\\\\","",$local)))
{
$isValid = false;
}
}
if ($isValid && !(checkdnsrr($domain,"MX") ||
?checkdnsrr($domain,"A")))
{
// domain not found in DNS
$isValid = false;
}
}
return $isValid;
}
资料来源:Douglas Lovell
答案 4 :(得分:0)
到目前为止,我看到的最佳电子邮件验证是(注意:它还会检查电子邮件域):
/**
* Validates an email address to RFC 3696 specification.
* @source http://www.linuxjournal.com/article/9585
* @param string $email_address Email address (raw input)
* @return <type> Returns true if the email address has the email address
* format and the domain exists.
*/
public static function email($email_address) {
if (empty($email_address)) return $email_address;
$is_valid = true;
$atIndex = strrpos($email_address, "@");
if (is_bool($atIndex) && !$atIndex) {
throw new VerificationException('The email address ('.$email_address.') does not contain an @ symbol');
$is_valid = false;
}
else {
$domain = substr($email_address, $atIndex+1);
$local = substr($email_address, 0, $atIndex);
$local_length = strlen($local);
$domain_length = strlen($domain);
if ($local_length < 1 || $local_length > 64) {
// Local part length exceeded
throw new VerificationException('The email address ('.$email_address.') local part exceeds maximum length');
} else if ($domain_length < 1) {
// Domain missing
throw new VerificationException('The email address ('.$email_address.') is mising the domain part');
} else if ($domain_length > 255) {
// Domain part length exceeded
throw new VerificationException('The email address ('.$email_address.') domain exceeds maximum length');
} else if ($local[0] == '.' || $local[$local_length-1] == '.') {
// Local part starts or ends with '.'
throw new VerificationException('The email address ('.$email_address.') local part can not end with a dot (.)');
} else if (preg_match('/\\.\\./', $local)) {
// Local part has two consecutive dots
throw new VerificationException('The email address ('.$email_address.') local part can not contain two consecutive dots (..)');
} else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
// Character not valid in domain part
throw new VerificationException('The email address ('.$email_address.') domain contains invalid characters');
} else if (preg_match('/\\.\\./', $domain)) {
// Domain part has two consecutive dots
throw new VerificationException('The email address ('.$email_address.') domain can not contain two consecutive dots (..)');
} else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) {
// Character not valid in local part unless
// Local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/',
str_replace("\\\\","",$local))) {
throw new VerificationException('The email address ('.$email_address.') contains invalid (non excaped) characters');
}
}
if ($is_valid && !(checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A'))) {
// Domain not found in DNS
throw new VerificationException('The email address ('.$email_address.') domain could not be found with a DNS lookup');
}
}
return $email_address;
}
答案 5 :(得分:-1)
您可能想要使用正则表达式。