【发布时间】:2012-02-10 20:34:24
【问题描述】:
背景:
我创建了一个显示不同国家货币汇率的网站。我使用 Google 的货币转换器 API 来执行计算。
http://www.google.com/ig/calculator?h1=en&q=9999gbp=?usd
请注意我传入的查询,例如9999 Great British Pounds 到 United States Dollars。
API 返回:
{lhs: "9999 British pounds",rhs: "15 769.4229 U.S. dollars",error: "",icc: true}
Google 在5 和7 之间用空格分隔15 769.4229。
当我在我的站点上返回计算结果时,这会导致问题,因为空格字符被替换为 � 符号。
请看下面的屏幕截图:
任何想法这个符号叫什么,所以我可以尝试删除它?
<?php
// Check to ensure that form has been submitted
if (isset($_POST['amount'], $_POST['from'], $_POST['to'])) {
$amount = trim($_POST['amount']);
$from = $_POST['from'];
$to = $_POST['to'];
try {
$conversion = currency_convert($googleCurrencyApi, $amount, $from, $to);
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage();
}
// Check URL has been formed
if ($conversion == false) {
echo 'Sorry, something went wrong';
} else {
echo $conversion[0], ' = ', $conversion[1];
}
}
function currency_convert($googleCurrencyApi, $amount, $from, $to) {
$result = file_get_contents($googleCurrencyApi . $amount . $from . '=?' . $to);
$expl = explode('"', $result);
if ($expl[1] == '' || $expl[3] == '') {
throw new Exception('An error has occured. Unable to get file contents');
} else {
return array(
$expl[1],
$expl[3]
);
}
}
?>
这是我目前的代码,所以你明白我的逻辑背后的想法。
【问题讨论】:
-
我猜问题只是编码。你试过
utf8_decode ( string $data )吗? -
$result = $conversion[0] . $conversion[1]; echo utf8_decode($result);仍然返回提到的字符
标签: php google-api