如何使用PHP的preg_match来验证字符串?

时间:2022-04-09 20:13:51

Please tell me how to use the various symbols used for expression matching in preg_match function of PHP.

请告诉我如何使用PHP preg_match函数中用于表达式匹配的各种符号。

Besides basic information, please give example for checking a valid e-mail address and a string that should not contain / : ; { } * &

除了基本信息外,请举例检查一个有效的电子邮件地址和一个不应该包含/:的字符串;{ } * &

5 个解决方案

#1


2  

Simple example.. Verifying $var which is a string verifying and checking for (a to z AND A to Z) characters.

简单的例子。验证$var,它是一个字符串,用于验证和检查(a到z和a到z)字符。

<?php
$var = 'hello';
if (ereg("[a-zA-Z]", $var)) {
  echo 'it was typed correctly';
} else {
  echo 'it was not typed correctly';
}
?>

more regular expressions syntax exemples: http://www.regexlib.com/

更多正则表达式语法示例:http://www.regexlib.com/

EDIT:

编辑:

if (ereg("^([0-9,a-z,A-Z]+)([.,_]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])?$", $email)) {

  echo 'email ok';

} else {

  echo 'email not ok';

}

Regards.

的问候。

#2


1  

Jeff Atwood recently had an article on his coding horror blog about regular expressions. Check out "Regular Expressions for Regular Programmers".

Jeff Atwood最近在他的编码恐怖博客上发表了一篇关于正则表达式的文章。查看“常规程序员的正则表达式”。

#3


0  

To check for a valid mail you can either use build in functionality (filter_var()/FILTER_VALIDATE_EMAIL) or use nice ready to use libraries which are compliant to the current RFC. PHP : Parsing Email Adresses in PHP. For examples on preg_match() you can go to the php website and a full list regular expression options is available on Wikipedia. To learn about Regex I recommend "The Regex Coach".

要检查有效的邮件,您可以使用build in功能(filter_var()/FILTER_VALIDATE_EMAIL)或使用nice准备使用符合当前RFC的库。PHP:解析PHP中的电子邮件地址。对于preg_match()的示例,您可以访问php网站,在Wikipedia上可以找到完整的列表正则表达式选项。要了解Regex,我推荐“Regex教练”。

#4


0  

I know the question is about PHP, but my purpose is to illustrate the intricacies of email address validation: To check if an email address conforms to RFC 2822, you can use the Perl module Email::Address. Do take a look at the source of that module as well as the RFC.

我知道这个问题是关于PHP的,但我的目的是说明电子邮件地址验证的复杂性:要检查电子邮件地址是否符合RFC 2822,您可以使用Perl模块电子邮件:::address。请查看该模块的源代码以及RFC。

#5


0  

If I give $var = 'hello87%^$' also returning "it was typed correctly";

如果我给$ var = " hello87% ^ $’也返回“输入正确”;

#1


2  

Simple example.. Verifying $var which is a string verifying and checking for (a to z AND A to Z) characters.

简单的例子。验证$var,它是一个字符串,用于验证和检查(a到z和a到z)字符。

<?php
$var = 'hello';
if (ereg("[a-zA-Z]", $var)) {
  echo 'it was typed correctly';
} else {
  echo 'it was not typed correctly';
}
?>

more regular expressions syntax exemples: http://www.regexlib.com/

更多正则表达式语法示例:http://www.regexlib.com/

EDIT:

编辑:

if (ereg("^([0-9,a-z,A-Z]+)([.,_]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])?$", $email)) {

  echo 'email ok';

} else {

  echo 'email not ok';

}

Regards.

的问候。

#2


1  

Jeff Atwood recently had an article on his coding horror blog about regular expressions. Check out "Regular Expressions for Regular Programmers".

Jeff Atwood最近在他的编码恐怖博客上发表了一篇关于正则表达式的文章。查看“常规程序员的正则表达式”。

#3


0  

To check for a valid mail you can either use build in functionality (filter_var()/FILTER_VALIDATE_EMAIL) or use nice ready to use libraries which are compliant to the current RFC. PHP : Parsing Email Adresses in PHP. For examples on preg_match() you can go to the php website and a full list regular expression options is available on Wikipedia. To learn about Regex I recommend "The Regex Coach".

要检查有效的邮件,您可以使用build in功能(filter_var()/FILTER_VALIDATE_EMAIL)或使用nice准备使用符合当前RFC的库。PHP:解析PHP中的电子邮件地址。对于preg_match()的示例,您可以访问php网站,在Wikipedia上可以找到完整的列表正则表达式选项。要了解Regex,我推荐“Regex教练”。

#4


0  

I know the question is about PHP, but my purpose is to illustrate the intricacies of email address validation: To check if an email address conforms to RFC 2822, you can use the Perl module Email::Address. Do take a look at the source of that module as well as the RFC.

我知道这个问题是关于PHP的,但我的目的是说明电子邮件地址验证的复杂性:要检查电子邮件地址是否符合RFC 2822,您可以使用Perl模块电子邮件:::address。请查看该模块的源代码以及RFC。

#5


0  

If I give $var = 'hello87%^$' also returning "it was typed correctly";

如果我给$ var = " hello87% ^ $’也返回“输入正确”;