Wednesday 22 May 2013

Convert 123456789 string to 9,8,7,6,5,4,3,2,1 without using string function

Hello every one.

I have another chalange in php. can you convert "123456789" string to "9,8,7,6,5,4,3,2,1" this string format without using string function.

first you tell this is not possible, but this is possible using array functions. See below code.

<?php

error_reporting(0);
$str="123456789";
$a=array();
for($i=0;$i<1000;$i++)
{
if($str[$i]!="")
{
array_push($a,$str[$i]);
}
else
{
break;
}
}
$exp=array_reverse($a);
$final="";
for($i=0;$i<count($exp);$i++)
{
if($i!=count($exp)-1)
{
$final.=$exp[$i].",";
}
else
{
$final.=$exp[$i];
}
}
echo $final."<br>";

?>

Using this code you will get "9,8,7,6,5,4,3,2,1".

Monday 27 August 2012

Currency Converter


Hello friends,

welcome back to my blog. If you are making a e-commerce website and you want to display amount in many more than one currency. for that one way that you have a current rate of particular currency and you make the function that return you amount in different currency. But in this way you have to check manually currency rate and store in some table from that you can access it. But this way is time wasting and not user friendly. The accrual system should get latest currency rate automatically so that administrator of website has no worry about the latest currency rate. So i have mentioned how to get latest currency rate and use it, let's use below code.


<?php
function get_currency($from,$to)
{
 $url = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s='. $from . $to .'=X';
 $handle = fopen($url, 'r');

 if($handle)
 {
     $result = fgets($handle, 4096);
     fclose($handle);
 }
 $allData = explode(',',$result); /* Get all the contents to an array */
 $curValue = $allData[1];
 return $curValue;
}
?>

you can see in this i have used yahoo's api, so it will get currency rate form yahoo and return you. But before using this be sure that  allow_url_fopen is on in your php.ini file. If it will off that it will return you warning. so first make allow_url_fopen on in your php.ini file.

As mentioned below you can call this function.

<?php
$dValue=get_currency("GBP","USD");
?>

Here the first parameter is from and second is to, as per this code this function will return 1 GBP= how many USD.

I hpe this will useful to you. If you have any query regarding this please tell me.

PHP Code to find location of website user


Hello friends,

In this post i have mentioned code to get current location of user of website.

First of all you have to fetch IP Address of user, use below code.

$_SERVER['REMOTE_ADDR']

Using this you will get IP address of user.

You have to make one class file.Below is code for it. Just copy and past it and make on class file named ip2locationlite.class.php



<?php
final class ip2location_lite{
    protected $errors = array();
    protected $service = 'api.ipinfodb.com';
    protected $version = 'v3';
    protected $apiKey = '111f531eafecfdbb35f990fcdaac5ce4023309b67a4065dd09c7481e40b63c0f';

    public function __construct(){}

    public function __destruct(){}

    public function setKey($key){
        if(!empty($key)) $this->apiKey = $key;
    }

    public function getError(){
        return implode("\n", $this->errors);
    }

    public function getCountry($host){
        return $this->getResult($host, 'ip-country');
    }

    public function getCity($host){
        return $this->getResult($host, 'ip-city');
    }

    private function getResult($host, $name){
        $ip = @gethostbyname($host);

        if(preg_match('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/', $ip)){
            $xml = @file_get_contents('http://' . $this->service . '/' . $this->version . '/' . $name . '/?key=' . $this->apiKey . '&ip=' . $ip . '&format=xml');

            try{
                $response = @new SimpleXMLElement($xml);

                foreach($response as $field=>$value){
                    $result[(string)$field] = (string)$value;
                }

                return $result;
            }
            catch(Exception $e){
                $this->errors[] = $e->getMessage();
                return;
            }
        }

        $this->errors[] = '"' . $host . '" is not a valid IP address or hostname.';
        return;
    }
}
?>


Now make one php page and write below code in it.


<?php
include('ip2locationlite.class.php');

//Load the class
$ipLite = new ip2location_lite;
$ipLite->setKey('111f531eafecfdbb35f990fcdaac5ce4023309b67a4065dd09c7481e40b63c0f');

//Get errors and locations
$locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);
print_r($locations);

?>

You will get array by this code. Array will contain status Code, status Message, ip Address, countryName, regionName, cityName, zipCode, latitude, longitude and time Zone.

If you want to access particular information like only city name, you have to pass key in array location. So your code will look like below.

for state code : $location['statusCode']
for status message : $location['statusMessage']
for ip address : $location['ipAddress']
for country code : $location['countryCode']
for country name : $location['countryName']
for state name : $location['regionName']
for city name : $location['cityName']
for zip code : $location['zipCode']
for latitude : $location['latitude']
for longitude : $location['longitude']
for time zone : $location['timeZone']

I hope this will helpful to you.

Difference between include() and require()


Hi Friends,

There are two function to include some file in your PHP script.

1)  include()
2)  require()

Both function do same thing, both are use for including some file to your PHP Code.
But basic difference is when you use include() function and if it failed to include file, it will give you worming, but if you use require() function and it failed to include file, it will show you fatal error.


So guys my advise to you that use include().

PHP Type Casting


PHP's type casting feature is amazing.

refer bellow example.

<?php
$a="10";
$b="1E1";

if($a==$b)
{
    echo "true";
}
else
{
    echo "false";
}
?>

so what will answer ? on first look you will defiantly say answer is false, but answer is true.

so now see how it is possible, described below.

In PHP if you take string "1E1" than if first letter is 0 it will consider 0, if first letter will 1 it will consider 1.
Now E1= exponent of 10= 10*1 so answer is 10.

So formula is : 1E1 = 1*10*1 =10.

so $a=$b. condition true.

But if you type === in if condition it will return false because it will also consider the type of variable.