PHP Forms Validate URL Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Form validation is a necessary process that must be done before submitting the data to the database. Any time a user submits a URL, it becomes crucial to investigate whether the given URL is valid or not. We can use PHP to validate the URLs that are submitted through an HTML form. Table of Content Using filter_var( ) FunctionUsing preg_match( ) FunctionUsing filter_var( ) FunctionTo validate a URL, the first approach is to use the filter_var( ) method with the FILTER_VALIDATE_URL filter. This method is widely used in PHP to filter a variable with a specific filter. It ensures that URLs are correctly formatted and secure, thus reducing the chances of security vulnerabilities. Example: Implementation to validate URL in PHP Forms using filter_var( ). PHP <?php // Taking a sample URL $url = "https://www.example.in"; // Checking if the URL is valid or not if (filter_var($url, FILTER_VALIDATE_URL)) { echo "$url is a VALID URL"; } else { echo "$url is a INVALID URL"; } ?> Outputhttps://www.example.in is a VALID URLUsing preg_match( ) FunctionThe preg_match( ) method is commonly used to search the given string using a pattern. This pattern is created using regular expression. Example: Implementation to validate URL in PHP Forms using preg_match( ) PHP <?php // Taking a sample URL $url = "https://www.example.in"; // Checking if the URL is valid or not if ( preg_match( "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $url ) ) { echo "$url is a VALID URL"; } else { echo "$url is a INVALID URL"; } ?> Outputhttps://www.example.in is a VALID URL Comment More infoAdvertise with us Next Article PHP Forms Validate URL H harshtondak Follow Improve Article Tags : PHP Web technologies PHP-Questions Similar Reads PHP Form Validation Form validation is a crucial step that needs to be completed before submitting data to the database. This process is performed to prevent any errors or faulty data from being inserted into the database. The HTML form includes various input fields such as email, text, checkbox, radio button, etc. The 7 min read How to Validate JSON in PHP ? Validating JSON in PHP refers to the process of checking whether a JSON string is properly formatted and valid according to JSON standards. In PHP, the json_decode() function is used to parse JSON, and validation can be checked using json_last_error() for errors.It is a PHP built-in function that is 3 min read PHP | parse_url() Function The parse_url() function is an inbuilt function in PHP which is used to return the components of a URL by parsing it. It parses an URL and return an associative array which contains its various components. Syntax: parse_url( $url, $component = -1 ) Parameters: This function accepts two parameters as 2 min read PHP urlencode() Function The urlencode() function is an inbuilt PHP function used to encode the URL. This function returns a string consisting of all non-alphanumeric characters exceptâ_. It is replaced by the percent (%) sign, followed by two hex digits and spaces encoded as plus (+) signs. Syntax:string urlencode( $input 1 min read PHP | DOMDocument validate() Function The DOMDocument::validate() function is an inbuilt function in PHP which is used to validate the document based on its DTD (Document Type Definition). DTD defines the rules or structure to be followed by the XML file and if a XML document doesn't follows this format then this function will return fa 2 min read Like