Showing posts with label PHP Array. Show all posts
Showing posts with label PHP Array. Show all posts

Friday, September 28, 2012

PHP Currency Converter with Google Currency Converter API


Are you looking for a reliable PHP Currency Converter, here is Google give you a solution from their open Google Currency Converter API


How this API Works:


This PHP Currency Converter work with following URL call back.



Querying Converter API from PHP function as per following URL:


q=1USD=?INR

http://www.google.com/ig/calculator?q=1USD=?INR

Based on Google Currency Converter API, I build an PHP Function to get Currency values from the out put of http://www.google.com/ig/calculator?hl=en&q=1USD=?INR. as PHP Array
function google_currency_converter($amount,$to,$from="INR")
{
 $content = file_get_contents("http://www.google.com/ig/calculator?hl=en&q=$amount."."$from=?$to"); 
 // Remove Spaces on API Output
 $content = preg_replace('/[^A-Za-z0-9.]+/','',$content);
 // Match All Currency values 0-9 and .(decimal)
 preg_match_all('/([0-9.]+)/', $content, $m);
 
 // Convert values with 3 decimal points
 $currency[$from] = sprintf("%.3f",$m[0][0]);
 $currency[$to] = sprintf("%.3f",$m[0][1]);
 
 return $currency;
}


$c = google_currency_converter(2800,"USD");

echo "INR:".$c['INR'];
echo "<br />";
echo "USD:".$c['USD'];



Friday, September 21, 2012

Convert Text Area to Array PHP



Processing multiple records through Text Area, We may need to convert that text area values into array from every line of text in that TextArea. here is my solution to "Convert TextArea to Array"



<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Text Area to Array</title>
</head>
<body>

<?php
// Check Form Submission
if(isset($_POST['submit']))
{
 // Capture cities text into variable
 $text = ucwords($_POST['cities']);
 // Replace entry new line with Comma(,)
 $cities = preg_replace("~\s*[\r\n]+~", ', ', $text);
 // Explode by Comma(,) and trim if any white spaces with array_map
 $cities = array_map('trim',explode(",",$cities));
 // final output as array
 echo "<pre>";
 print_r($cities);
 echo "</pre>";
}


?>

<form action="" method="post">
  <h2>Convert Text Area to Array</h2>
  <p>Enter each city every line<br />
    <textarea name="cities" cols="50" rows="10" id="keywords"></textarea>
    <br />
    <br />
    
    <input name="submit" type="submit" value="Submit Cities" id="submit" />
  </p>
</form>
</body>
</html>



Download This Script     Live Demo    

Wednesday, January 18, 2012

PHP Sort array by key values based on another array

When you are working with array and data field sets, sorting array by key value is very usefull to reorder data set based on your requirement.

Here is a PHP function to Sort array by key values based on another array