PHP regular expression to verify name format
In a web application, it is necessary to verify whether the name format entered by the user is correct. For this purpose, regular expressions can be used for validation. This article explains how to use regular expressions in PHP to verify name format.
1. Rules for name format
Generally speaking, the format of a Chinese name is "name (sometimes you need to add a surname)". The format of English names is "last name, first name". For Chinese names, they are generally composed of two Chinese characters, and the first Chinese character is the surname, and the second Chinese character is the first name; for English names, there is usually a comma separating the surname and the given name. It should be noted that some countries and regions have different English name formats.
2. Regular expressions in PHP
The preg_match() function in PHP can be used to search whether there is content matching the regular expression in the string. The usage of the preg_match() function is as follows:
preg_match(pattern,string);
where pattern is a regular expression and string is the string to be searched. If there is content matching pattern in string, the preg_match() function will return 1, otherwise it will return 0.
The following is a regular expression used to verify the Chinese name format:
/^[x{4e00}-x{9fa5}]{2,5}$/u
The meaning of this regular expression is: starting and ending with 2 to 5 Chinese characters, with no other characters in between. Among them, x{4e00}-x{9fa5} represents the Chinese character range.
The following is a regular expression used to verify the English name format:
/^([a-zA-Z] )s,s([a- zA-Z] )$/u
The meaning of this regular expression is: with commas as delimiters, the left side is the last name composed of one or more English characters, and the right side is the last name composed of one or more English characters composed name. Among them, s represents a blank character, and * represents repeated 0 or more times.
3. PHP code example
The following is a PHP code example that uses regular expressions to verify the name format:
<?php function checkName($name){ if(preg_match('/^[x{4e00}-x{9fa5}]{2,5}$/u',$name)||preg_match('/^([a-zA-Z]+)s*,s*([a-zA-Z]+)$/u',$name)){ return true; }else{ return false; } } ?>
In the above code, the checkName() function accepts a $ name parameter, which is the name to be verified. Two regular expressions are used in the function to verify the Chinese name and English name format. If $name matches one of the regular expressions, the function will return true, otherwise it will return false. You can put the above code in a separate file or use it directly in the web page.
4. Summary
This article introduces how to use regular expressions in PHP to verify the name format. It should be noted that the name format may be different in different countries and regions and needs to be adjusted according to the actual situation. In actual development, security and other factors also need to be taken into consideration to conduct comprehensive input verification and filtering.
The above is the detailed content of PHP regular expression to verify name format. For more information, please follow other related articles on the PHP Chinese website!