Category Archives: Uncategorized

How to get array length in php?

It’s as follows :-

<?php
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
$result = count($a);       //we get the length of array here.
?>

PHP class to generatre UUID

It can be written as follows :-

    class uuidgen
    {
        function uuid($serverID=1)
        {
            $t=explode(" ",microtime());
            return sprintf( '%04x-%08s-%08s-%04s-%04x%04x',
                $serverID,
                $this->clientIPToHex(),
                substr("00000000".dechex($t[1]),-8),   // get 8HEX of unixtime
                substr("0000".dechex(round($t[0]*65536)),-4), // get 4HEX of microtime
                mt_rand(0,0xffff), mt_rand(0,0xffff));
        }
        function uuidDecode($uuid)
        {
            $rez=Array();
            $u=explode("-",$uuid);
            if(is_array($u)&&count($u)==5)
            {
                $rez=Array(
                    'serverID'=>$u[0],
                    'ip'=>$this->clientIPFromHex($u[1]),
                    'unixtime'=>hexdec($u[2]),
                    'micro'=>(hexdec($u[3])/65536)
                );
            }
            return $rez;
        }
        function clientIPToHex($ip="")
        {
            $hex="";
            if($ip=="")
            {
                $ip=getEnv("REMOTE_ADDR");
            }
            $part=explode('.', $ip);
            for ($i=0; $i<=count($part)-1; $i++)
            {
                $hex.=substr("0".dechex($part[$i]),-2);
            }
            return $hex;
        }
        function clientIPFromHex($hex)
        {
            $ip="";
            if(strlen($hex)==8)
            {
                $ip.=hexdec(substr($hex,0,2)).".";
                $ip.=hexdec(substr($hex,2,2)).".";
                $ip.=hexdec(substr($hex,4,2)).".";
                $ip.=hexdec(substr($hex,6,2));
            }
            return $ip;
        }
    }

To get uuid call the class as follows :-


$x=$uuid->uuid();
echo $x

How to replace a content using regular expression search in php?

It’s as follows :-

 <?php
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string);
?> 
O/p :-
April1,2003

How to replace a content in php?

It’s as folllows : –


<?php
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
?>

Add one day additional to a date in php

It’s as follows : –

 

$year=2013;
$month=04;
$date=12;
$newdate=date('Y-m-d H:i:s', strtotime($year."-".$month."-".$date." 00:00:00" . ' + 1 day'));

O/p :-

$new date will be “2013-04-13 00:00:00