Home Backend Development PHP Tutorial PHP data filtering: validating email and URL input

PHP data filtering: validating email and URL input

Jul 28, 2023 pm 01:45 PM
filter verify enter

PHP Data Filtering: Verify Email and URL Input

In web development, data filtering and validation are very important. Data filtering can help us eliminate invalid or dangerous input and effectively protect the security and stability of the system. This article will focus on how to use PHP to validate email and URL input.

1. Verification of email input

Verification of email input is a common requirement, which we can achieve with the help of PHP regular expressions. The following is a sample code that demonstrates how to verify the validity of email input:

$email = "example@example.com";

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "邮箱地址无效";
} else {
    echo "邮箱地址有效";
}
Copy after login

In the above code, the filter_var function and the FILTER_VALIDATE_EMAIL filter are used to determine whether the $email variable is a valid email address. If the verification passes, "Email address is valid" is output, otherwise "Email address is invalid" is output.

2. Verify URL input

Verification of URL input is also a very common requirement, and there are corresponding functions and filters in PHP to implement it. The following is a sample code that demonstrates how to verify the validity of a URL input:

$url = "https://www.example.com";

if (!filter_var($url, FILTER_VALIDATE_URL)) {
    echo "URL地址无效";
} else {
    echo "URL地址有效";
}
Copy after login

In the above code, the filter_var function and the FILTER_VALIDATE_URL filter are also used to determine whether the $url variable is a valid URL address. If the verification passes, "URL address is valid" is output, otherwise "URL address is invalid" is output.

3. Custom filtering rules

PHP’s data filtering function also supports custom filtering rules, which can be set according to specific needs. The following is a sample code that demonstrates how to customize a filtering rule to verify whether the entered age is a positive integer:

$age = "20";

$customFilter = array(
    'options' => array(
        'min_range' => 1,
        'max_range' => 120
    )
);

if (!filter_var($age, FILTER_VALIDATE_INT, $customFilter)) {
    echo "年龄无效";
} else {
    echo "年龄有效";
}
Copy after login

In the above code, the filter_var function and the FILTER_VALIDATE_INT filter are used to determine whether the $age variable is A valid integer, and a custom filtering rule is set through the $customFilter array: ensure that the value of $age ranges from 1 to 120. If the verification passes, "age is valid" is output, otherwise "age is invalid" is output.

To sum up, PHP provides a powerful data filtering function, which can verify common inputs such as emails and URLs through built-in filters, and supports custom filtering rules. Proper use of these functions can help us filter and verify the validity of input data and improve the security and stability of the system.

The above is the detailed content of PHP data filtering: validating email and URL input. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Disabling Win11 Input Experience Guide Disabling Win11 Input Experience Guide Dec 27, 2023 am 11:07 AM

Recently, many Win11 users have encountered the problem that the input experience dialog box always flickers and cannot be turned off. This is actually caused by the default system services and components of Win11. We need to disable the relevant services first, and then disable the input experience service. Solved, let’s try it out together. How to turn off the input experience in win11: First step, right-click the start menu and open "Task Manager". Second step, find the three processes "CTF Loader", "MicrosoftIME" and "Service Host: Textinput Management Service" in order, right-click "End Task" "The third step, open the start menu, search and open "Services" at the top. The fourth step, find "Textinp" in it

Windows input encounters hang or high memory usage [Fix] Windows input encounters hang or high memory usage [Fix] Feb 19, 2024 pm 10:48 PM

The Windows input experience is a key system service responsible for processing user input from various human interface devices. It starts automatically at system startup and runs in the background. However, sometimes this service may automatically hang or occupy too much memory, resulting in reduced system performance. Therefore, it is crucial to monitor and manage this process in a timely manner to ensure system efficiency and stability. In this article, we will share how to fix issues where the Windows input experience hangs or causes high memory usage. The Windows Input Experience Service does not have a user interface, but it is closely related to handling basic system tasks and functions related to input devices. Its role is to help the Windows system understand every input entered by the user.

How to verify signature in PDF How to verify signature in PDF Feb 18, 2024 pm 05:33 PM

We usually receive PDF files from the government or other agencies, some with digital signatures. After verifying the signature, we see the SignatureValid message and a green check mark. If the signature is not verified, the validity is unknown. Verifying signatures is important, let’s see how to do it in PDF. How to Verify Signatures in PDF Verifying signatures in PDF format makes it more trustworthy and the document more likely to be accepted. You can verify signatures in PDF documents in the following ways. Open the PDF in Adobe Reader Right-click the signature and select Show Signature Properties Click the Show Signer Certificate button Add the signature to the Trusted Certificates list from the Trust tab Click Verify Signature to complete the verification Let

Detailed method to unblock using WeChat friend-assisted verification Detailed method to unblock using WeChat friend-assisted verification Mar 25, 2024 pm 01:26 PM

1. After opening WeChat, click the search icon, enter WeChat team, and click the service below to enter. 2. After entering, click the self-service tool option in the lower left corner. 3. After clicking, in the options above, click the option of unblocking/appealing for auxiliary verification.

How to open filtered duplicate files in Quark How to open filtered duplicate files in Quark Mar 01, 2024 am 11:25 AM

When using Quark Browser, there is a function to filter duplicate files. Some friends are not very familiar with this. Here I will introduce how to turn on this function. If you are interested, come and take a look with me. 1. First, click "Quark Browser" on your mobile phone to enter the interface, then click and select "Quark Network Disk" in the options in the middle of the page to open and enter. 2. Find "Backup Settings" in the lower part of the Quark network disk interface, and click to open it, as shown in the figure below: 3. Next, on the page you enter, there is a "Filter Duplicate Files", which is displayed behind it There is a switch button. Click the circular slider on it and set it to color to turn on this function. When you continue to back up files, duplicate files will be skipped to save network disk capacity.

Python implements XML data filtering and filtering Python implements XML data filtering and filtering Aug 09, 2023 am 10:13 AM

Python implements XML data filtering and filtering. XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. It is flexible and scalable and is often used for data exchange between different systems. When processing XML data, we often need to filter and filter it to extract the information we need. This article will introduce how to use Python to filter and filter XML data. Import the required modules Before starting, we

How to validate IFSC code using regular expressions? How to validate IFSC code using regular expressions? Aug 26, 2023 pm 10:17 PM

Indian Financial System Code is the abbreviation. Indian bank branches participating in the electronic funds transfer system are identified by a special 11-character code. The Reserve Bank of India uses this code in internet transactions to transfer funds between banks. IFSC code is divided into two parts. Banks are identified by the first four characters, while branches are identified by the last six characters. NEFT (National Electronic Funds Transfer), RTGS (Real Time Gross Settlement) and IMPS (Immediate Payment Service) are some of the electronic transactions that require IFSC codes. Method Some common ways to validate IFSC codes using regular expressions are: Check if the length is correct. Check the first four characters. Checkthefifthcharacter.Che

New features in PHP 8: Added verification and signing New features in PHP 8: Added verification and signing Mar 27, 2024 am 08:21 AM

PHP8 is the latest version of PHP, bringing more convenience and functionality to programmers. This version has a special focus on security and performance, and one of the noteworthy new features is the addition of verification and signing capabilities. In this article, we'll take a closer look at these new features and their uses. Verification and signing are very important security concepts in computer science. They are often used to ensure that the data transmitted is complete and authentic. Verification and signatures become even more important when dealing with online transactions and sensitive information because if someone is able to tamper with the data, it could potentially

See all articles