Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How PHP validates messages and URL in forms

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how PHP verifies mail and URL in the form". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Regular expression

Regular expression is a way to describe the rules of a paragraph of text, it is not an exact match, but through some specific symbols to fuzzy match. In PHP, we use the preg_match function to perform regular expression matching, one parameter is our regular expression rule, and the second parameter is the text to check.

The syntax format of the preg_match function is as follows:

Preg_match (string $regular, string $string [, array & $result])

What we need to notice is that the $string variable is matched according to the $regular variable. If it exists, the number of matches is returned, and the matching results are put in the $result variable. Returns 0 if there is no match to the result. ^ means the beginning; $means the end.

Next, let's look at the application of regular expressions through an example:

In the above example, you are matching a number that starts with date. Output result:

Next, let's take a look at the matching content of the third parameter of preg_matchede. Usually we will pass in an empty array, because it is an address call, and when the matching is over, the array will get the specific matching content.

Let's take a look at an example, which is as follows:

Output result:

In regular expressions, letters are represented by\ w, while numbers are represented by\ d (\ D means non-numeric).

+ means one or more, * means 0 or more,? It means yes or no, {n} means specific bits, and {m, n} means more than m and less than n.

Examples are as follows:

Output result:

Use or conditions can be used to match strings, if it is only a single letter or character, you can use range representation, and [] can indicate the value range of a character'/ [a0\.] / 'can match contains an or 0 or. Gets or sets any string of the

In addition, regular expressions can use-to represent a set of ranges, [amurz] for any of the 26 lowercase letters, [Amurz] for an uppercase letter, and [0-9] for a decimal number.

With so much knowledge about regular expressions, it's time to verify that the form is in the correct format.

PHP verify name, email, and URL

Validation rules for names are required and can only contain letters and spaces. Validation rules for E-mail are required, which must be a valid e-mail address (including'@ 'and'.'). The URL's validation rule is optional, and if it exists, it must contain a valid URL. The validation rules for the remarks are optional, with multiple lines of input fields. Gender verification rules are necessary and one must be selected.

Next, we can write the following code through the policy expression in the above. We will check whether the name field contains letters and spaces in a simple way. If the value of the name field is invalid, an error message will be output. The example is as follows:

$name = test_input ($_ POST ["name"]); if (! preg_match ("/ ^ [a-zA-Z] * $/", $name)) {$nameErr = "only letters and spaces allowed";}

Through the above code, we can prompt the user to fill in the format of the name, and then we can verify the email and URL in the same way.

The mailbox name can be any character composed of letters, numbers, underscores and dots; the mailbox should contain the @ symbol, and the following text will be processed according to the domain name rules. The following code will check whether the e-mail address is valid in a simple way. If the e-mail address is invalid, an error message will be output, as shown in the following example:

$email = test_input ($_ POST ["email"]); if (! preg_match ("/ ^ [a-zA-Z0-9 formats -] + @ [a-zA-Z0-9 formats -] + (\ .[ a-zA-Z0-9 cycles -] +) + $/", $email) {$emailErr = "invalid email format!" ;}

The following code detects whether the URL address is valid (the following regular expression runs URL with a dash: "-"), and if the URL address is illegal, an error message will be output, as shown in the following example:

$website = test_input ($_ POST ["website"]); if (! preg_match ("/\ b (?: (?: https? | ftp):\ /\ / | www\.) [- a-z0-9 invalid URL #\ /%? = ~ _ |,.;] * [- a-z0-9 invalid URL #\ /% = ~ _], $website) {$websiteErr =" invalid URL ";}

Add the above verification method to the overall format. The example is as follows:

.error {color: # FF0000;} PHP verification instance

* required fields

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report