背景:
我创建了一个显示来自不同国家的货币汇率的网站.我使用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}
谷歌已经分离15 769.4229与之间的空白5和7.
当我在我的网站上返回计算结果时,这会导致问题,因为白色空格字符用 符号替换.
见下面的屏幕截图:

任何想法这个符号被称为什么,所以我可以尝试删除它?
<?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]
);
}
}
?>
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我的代码,所以你可以了解我的逻辑背后的想法.
这很可能是不间断的空格,请尝试替换为空格:
$result = file_get_contents($googleCurrencyApi . $amount . $from . '=?' . $to);
$result = str_replace("\xA0", " ", $result);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
975 次 |
| 最近记录: |