<?php echo convertMoney("GBP", 3); function convertMoney($targetCurrency, $price) { if($targetCurrency != "EUR") { return str_replace(",", ".", _getMoneyFromGoogle($targetCurrency, $price)); } return $price; } function _getMoneyFromGoogle($targetCurrency, $price) { $matches = Array(); $response = file_get_contents( "http://www.google.com/ig/calculator?hl=de&q=". $price."EUR%3D%3F".$targetCurrency, true); preg_match('/rhs:\s*"([^"]+)"/', $response, $matches); //json string is not correctly formated, do it manually $result = $matches[1]; $result = ereg_replace("[^0-9,]*", "", $result); //currency name is still part of string, // remove everything than numbers and comma return $result; } ?>
Hack a tool is a blog which provides links and information about tools we wrote to make our life as programmers easier.
Showing posts with label php. Show all posts
Showing posts with label php. Show all posts
Tuesday, May 28, 2013
Convert money with google API
This is a small example on how to use the google API to convert euros in any given currency. The parameters are the target currency in a 3 letter code and the float value in euros. If you want to use a different source currency (maybe as a parameter), search for the "EUR" in the URL.
Wednesday, March 14, 2012
Import Excel xls in PHP
This article is based on a great project two guys did on google code:
http://code.google.com/p/php-excel-reader/
It is opening Excel files without any additional driver like an ODBC connector. I am extending the only - very small - example with another one importing the data of the attached spread sheet to a mysql database.
This is the simple structure of the table:
I have an additional note to this API: formated numerical values (currency, ...) are maybe not displayed correctly. This is maybe due to wrong interpretion of the format string. You can change to use the function "raw" instead of "val" but the string will not be formated.
This article and code is licensed under MIT license
CREATE TABLE IF NOT EXISTS `table1` ( `value1` int(10) NOT NULL, `value2` varchar(255) NOT NULL, `value3` varchar(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;And the code importing the data. You first need to choose the sheet and afterwards the data is directly imported.
<?php error_reporting(E_ALL ^ E_NOTICE); require_once 'excel_reader2.php'; $data = new Spreadsheet_Excel_Reader("testdata.xls"); ?> <html> <head> </head> <body> <form method="POST"> <?php if(isset($_REQUEST["sheet"])) { $sheet = $_REQUEST["sheet"]; $db = mysql_connect("localhost", "root", "password") or die("error in mysql_connect"); mysql_select_db('excel', $db) or die("error in mysql_select_db"); $rowcount = $data->rowcount($sheet); for($i = 2; $i < $rowcount; ++$i) //rows are 1 based, first row is header { $query = "INSERT INTO table1 (`value1`, `value2`, `value3`) VALUES (".$data->val($i, 1, $sheet).", '".$data->val($i, 2, $sheet)."', '".$data->val($i, 3, $sheet)."')"; mysql_query($query, $db) or die("error in mysql_query"); } mysql_close($db); } else { ?> <select name="sheet"> <?PHP foreach($data->boundsheets as $key => $row) { ?> <option value="<?PHP echo $key; ?>"> <?PHP echo $row["name"]; ?></option> <?PHP } ?> </select> <input type="submit" value="import" /> <?PHP } ?> </form> </body> </html>This is the content of the file:
I have an additional note to this API: formated numerical values (currency, ...) are maybe not displayed correctly. This is maybe due to wrong interpretion of the format string. You can change to use the function "raw" instead of "val" but the string will not be formated.
This article and code is licensed under MIT license
Monday, October 31, 2011
PHP UTF-8 character to dezimal converter
A while ago I was developing on a connector to a SMS sending API. As SMS are partially transmitted in a 7-bit coded limited character set (http://en.wikipedia.org/wiki/GSM_03.38) not all characters are supported. To validate each and every character in the message, it is useful - at least for debugging purposes - to convert the characters to decimal and filter it by looking up an array.
Correct me if I am wrong but in my opinion, PHP is not really supporting development with UTF-8 this is why my function is checking the bytes low-level.
Simply change the variable $string to the text you want to convert and the output will be value and character row by row.
Correct me if I am wrong but in my opinion, PHP is not really supporting development with UTF-8 this is why my function is checking the bytes low-level.
Simply change the variable $string to the text you want to convert and the output will be value and character row by row.
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us" dir="ltr" > <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> </head> <body>
<?php $string = "|^€{}[~]\\"; $count = 0; for ($i=0; $i < strlen($string); $i++) { echo ordUTF8($string, $i, $count)." ".$string[$i]."<br />"; $i += $count - 1; } function ordUTF8($string, $index = 0, &$bytes = null) { $len = strlen($string); $bytes = 0; if ($index >= $len) { return false; } $h = ord($string{$index}); if ($h <= 0x7F) { $bytes = 1; return $h; } else if ($h < 0xC2) { return false; } else if ($h <= 0xDF && $index < $len - 1) { $bytes = 2; return ($h & 0x1F) << 6 | (ord($string{$index + 1}) & 0x3F); } else if ($h <= 0xEF && $index < $len - 2) { $bytes = 3; return ($h & 0x0F) << 12 | (ord($string{$index + 1}) & 0x3F) << 6 | (ord($string{$index + 2}) & 0x3F); } else if ($h <= 0xF4 && $index < $len - 3) { $bytes = 4; return ($h & 0x0F) << 18 | (ord($string{$index + 1}) & 0x3F) << 12 | (ord($string{$index + 2}) & 0x3F) << 6 | (ord($string{$index + 3}) & 0x3F); } else { return false; } } ?>
</body> </html>
Subscribe to:
Posts (Atom)
