url, email and ip validation with php

Simple PHP functions to validate url, email address and ip, functions will return TRUE for valid data and FALSE for invalid data.

PHP URL validation.

A simple php function to check if the given URL is valid or not using preg_match..
1
2
3
4
5
6
7
<?php 
 
function is_valid_url($url){
    $p1 ='/(http|https|ftp):\/\/[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(\/.*)?$/i';
    return preg_match($p1, $url); 
}
?>


PHP Email validation.

A simple php function to check if the given email is valid or not using preg_match.
1
2
3
4
5
6
7
<?php
 
function is_valid_email($email){
    $p1 = '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-z]{2,4}$/i';
    return preg_match($p1, $email);
}
?>

Another php function to check if the given email is valid or not using FILTER_VALIDATE_EMAIL.
1
2
3
4
5
6
7
8
9
<?php 
 
function is_valid_email($email){
    if (filter_var($email, FILTER_VALIDATE_EMAIL))
        return true;
    else
        return false;
}
?>



PHP IP validation.

A simple php function to check if the given IP address is valid or not using preg_match.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php 
 
function is_valid_ip($ip) {
    $p1='/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/';
    if (preg_match($p1, $ip)){
        $parts = explode('.', $ip);
        for ($i=0; $i<4; $i++)
            if ($parts[$i] > 255) 
                return 0;
        return 1;
    }
    else return 0; 
}
?>

Another php function to check if the given IP address is valid or not using FILTER_VALIDATE_IP.
1
2
3
4
5
6
7
8
9
<?php 
 
function is_valid_ip($ip){
    if (filter_var($ip, FILTER_VALIDATE_IP))
        return true;
    else
        return false;
}
?>



If this post was good and helpful for you, Please give it Like.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment