我正在尝试将货币从 USD 转换为 INR 我使用以下代码:
function currency($from_Currency,$to_Currency,$amount) {
$amount = urlencode($amount);
$from_Currency = urlencode($from_Currency);
$to_Currency = urlencode($to_Currency);
$url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$rawdata = curl_exec($ch);
curl_close($ch);
$data = explode('"', $rawdata);
$data = explode('"', $data['3']);
$var = $data[0];
$var=str_replace('Indian rupees','',$var);
$var=str_replace(' ', '', $var);
$var = preg_replace('/\s+/', '', $var);
//return floatval($var);
return $var;
}
我希望返回值不受货币单位和空格的影响,我想要一个完美的浮点值。我通过 str_replace() 删除了货币单位,但无法删除空间,因为使用 floatval() 转换为浮点数没有按预期工作。请帮我。