PHP str_replace 和/或 number_format 失败

发布于 2024-12-08 17:26:48 字数 1359 浏览 0 评论 0原文

      $BRL_PRICE = currency("USD", "BRL", $allmoney);
      echo "BRL1 = " . $BRL_PRICE."<br />";
      $BRL_PRICE = str_replace(" ", "", $BRL_PRICE);
      echo "BRL2 = " . $BRL_PRICE."<br />";  
      $BRL_PRICE = number_format($BRL_PRICE, 2);
      echo "BRL3 = " . $BRL_PRICE."<br />";

输出是..

BRL1 = 1 531.70922
BRL2 = 1 531.70922
BRL3 = 1.00

我真正想要的是将值四舍五入到最接近的美分..10位。我知道 number_format 失败,因为currency() google Money Converter 添加了一个空格而不是 comra 或空白。

我想

BRL3 = 1531.71

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'];
    return $var;
}
      $BRL_PRICE = currency("USD", "BRL", $allmoney);
      echo "BRL1 = " . $BRL_PRICE."<br />";
      $BRL_PRICE = str_replace(" ", "", $BRL_PRICE);
      echo "BRL2 = " . $BRL_PRICE."<br />";  
      $BRL_PRICE = number_format($BRL_PRICE, 2);
      echo "BRL3 = " . $BRL_PRICE."<br />";

outputs are..

BRL1 = 1 531.70922
BRL2 = 1 531.70922
BRL3 = 1.00

All I really want is to round up the value to the nearest cents.. 10s place. I know number_format failures because currency() google money converter adds a space instead of a comra or blank.

I want

BRL3 = 1531.71

.

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'];
    return $var;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

活泼老夫 2024-12-15 17:26:48

如果您懒得阅读文档,请不要责怪软件:

$BRL_PRICE = number_format($BRL_PRICE, 2, '.', '');

echo number_format(1531.70922, 2, '.', '');

Don't blame software in cases when you was just too lazy to read documentation:

$BRL_PRICE = number_format($BRL_PRICE, 2, '.', '');

echo number_format(1531.70922, 2, '.', '');
So要识趣 2024-12-15 17:26:48

为什么不使用 sprintf('%.2f',$var)

Why not use sprintf('%.2f',$var)

花开雨落又逢春i 2024-12-15 17:26:48

我不认为你的号码中有一个简单的空格。尝试 preg_replace 看看会发生什么:

$BRL_PRICE = preg_replace('/\s+/', '', $BRL_PRICE);

I don't think it is a simple space in your number. Try preg_replace and see what happens:

$BRL_PRICE = preg_replace('/\s+/', '', $BRL_PRICE);
反话 2024-12-15 17:26:48

您从 google 获得的响应是​​无效的 json。你甚至不需要json_decode它,而只是为了日志。

str_replace 工作得很好(一如既往),您只需替换正确的字符串:

$BRL_PRICE = str_replace("\xc2\xa0", "", $BRL_PRICE);

即 Unicode 字符“NO-BREAK SPACE”(U+00A0)。

The response you get from google is invalid json. You don't even json_decode it, but just for the log.

str_replace works pretty well (as always), you just need to replace the right string:

$BRL_PRICE = str_replace("\xc2\xa0", "", $BRL_PRICE);

That is the Unicode Character 'NO-BREAK SPACE' (U+00A0).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文