Home > Backend Development > PHP Tutorial > PHP regular expression to verify name format

PHP regular expression to verify name format

王林
Release: 2023-06-24 13:56:01
Original
2064 people have browsed it

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;
    }
}
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template