Thursday, 8 September 2016

PHP Function to Validate IP Address

Simple php function to validate IP address and check whether it is in private network range. Ip validation in php for easy validating of IPv4 address.

function validateIpAddress($ip) {
    if(strtolower($ip)==='unknown' || $ip=="") return false;
    $ip = ip2long($ip); // get ipv4 network address
    if($ip!==false && $ip!==-1) {
        $ip = sprintf('%u', $ip);
        // check private network range
        if ($ip >= 0 && $ip <= 50331647) return false;
        if ($ip >= 167772160 && $ip <= 184549375) return false;
        if ($ip >= 2130706432 && $ip <= 2147483647) return false;
        if ($ip >= 2851995648 && $ip <= 2852061183) return false;
        if ($ip >= 2886729728 && $ip <= 2887778303) return false;
        if ($ip >;= 3221225984 && $ip <= 3221226239) return false;
        if ($ip >;= 3232235520 && $ip <= 3232301055) return false;
        if ($ip >= 4294967040) return false;
    }
    return true;
}

Wednesday, 7 September 2016

Email Address Validation in PHP using Regex

Simple regex to validate email address in php. Validate email address in php regex while submitting form.

$email_regex = "/([a-z0-9_]+|[a-z0-9_]+\.[a-z0-9_]+)@(([a-z0-9]|[a-z0-9]+\.[a-z0-9]+)+\.([a-z]{2,4}))/i";
if(!preg_match($email_regex, $email)) {
    echo "Invalid Email Address";
}
else {
    echo "Valid Email Address";
}

Monday, 5 September 2016

Simple Time Ago Display Function in PHP

Make use of this simple function to display time ago format details of the time in your comment system. Facebook style time ago display script in simple php.

function timeAgo($date){
    $time = strtotime($date);
    $now = time();
    $ago = $now - $time;
    if($ago<60) {
        $timeago = round($ago);
        $s = ($timeago == 1)?"second":"seconds";
        return "$timeago $s ago";
    } elseif($ago<3600) {
        $timeago = round($ago / 60);
        $m = ($timeago == 1)?"minute":"minutes";
        return "$timeago $m ago";
    } elseif($ago>=3600 && $ago<86400) {
        $timeago = round($ago / 60 / 60);
        $h = ($timeago == 1)?"hour":"hours";
        return "$timeago $h ago";
    } elseif($ago>=86400 && $ago<2629743.83) {
        $timeago = round($ago / 60 / 60 / 24);
        $d = ($timeago == 1)?"day":"days";
        return "$timeago $d ago";
    } elseif($ago>=2629743.83 && $ago<31556926) {
        $timeago = round($ago / 60 / 60 / 24 / 30.4375);
        $m = ($timeago == 1)?"month":"months";
        return "$timeago $m ago";
    } else {
        $timeago = round($ago / 60 / 60 / 24 / 365);
        $y = ($timeago == 1)?"year":"years";
        return "$timeago $y ago";
    }
}

Sunday, 4 September 2016

Simple Header and Meta Redirection in PHP

Refresh/Redirect management to different pages in PHP. Simple function to help in refreshing and redirecting a page in PHP. This function helps in redirection by different ways. Header redirection in PHP is a efficient method. We can use meta redirection to redirect and to refresh a page in PHP.

function redirect($url,$header=false,$time=0) {
    if(!headers_sent() && $header) {
        header( 'refresh:'.$time.'; url='.$url.'');
        exit;
    }
    else {
        echo '<script type="text/javascript">';
        echo 'window.setTimeout(function() {
                window.location.href="'.$url.'";
            }, '.($time*1000).');';
        echo '</script>';
        echo '<noscript>';
        echo '<meta http-equiv="refresh" content="'.$time.';url='.$url.'" />';
        echo '</noscript>';
        exit;
    }
}

Saturday, 3 September 2016

Explode a String by Multiple Delimiters in PHP

Explode a string using multiple delimiters in php. multipleExplode function helps in spliting a string by multiple delimiters in php.

function multipleexplode($delimiters,$string) {
    $ready = str_replace($delimiters, $delimiters[0], $string);
    $array = explode($delimiters[0], $ready);
    return  $array;
}

Friday, 2 September 2016

File Size Calculator in PHP

Calculate the size of a file from a URL and display the size in respective units. This function takes a file url and calculates the size the file in php.

function calculateFileSize($url){
    $size = filesize($url);
    if($size>=1073741824) {
        $fileSize = round($size/1024/1024/1024,1).'GB';
    } elseif($size>=1048576) {
        $fileSize = round($size/1024/1024,1).'MB';
    } elseif($size>=1024) {
        $fileSize = round($size/1024,1).'KB';
    } else {
        $fileSize = $size.' bytes';
    }
    return $fileSize;
}

Thursday, 1 September 2016

Simple Tax Calculator in PHP

Tax Calculator for calculating the tax of a product in php. Make use of this php calculator to calculate tax of any country by passing in product rate and tax percentage.

function taxCalculator($amount,$tax_rate){
    $tax_rate = $tax_rate/100;
    $tax = $amount * $tax_rate;
    return $total = $tax + $amount;
}