char returned from Google Currency API

Background:

I have created a website that displays exchange rates from different countries. I use the Google Currency Converter API to do the calculations.

http://www.google.com/ig/calculator?h1=en&q=9999gbp=?usd

Pay attention to the request that I submitted, for example. 9999 Great British Pounds to United States Dollars .

The API returns:

{lhs: "9999 British pounds",rhs: "15 769.4229 US dollars",error: "",icc: true}

Google separated 15 769.4229 with a space between 5 and 7 .

This causes a problem when I return the calculation results on my site, when the char space is replaced by a character ..

See screenshot below:

enter image description here

Any ideas what this symbol is called, can I try to remove it?

 <?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] ); } } ?> 

Here is my code at the moment, so you understand my logic.

+6
source share
3 answers

This is most likely inextricable space, try replacing in space:

 $result = file_get_contents($googleCurrencyApi . $amount . $from . '=?' . $to); $result = str_replace("\xA0", " ", $result); 
+3
source

Try changing the code so that UTF8 decrypts the data returned from Google:

 // Check URL has been formed if ($conversion == false) { echo 'Sorry, something went wrong'; } else { echo utf8_decode($conversion[0]), ' = ', utf8_decode($conversion[1]); } 

I assume your standard encoding is ISO-8859-1

EDIT

(According to the comments) The problem may be that a null character was sent to you. Try the following:

 $result = str_replace("\0", "", $result ); 

or

 $result = str_replace("&#x0;", "", $result ); 
+1
source

After str_replace do:

 $result_amt = utf8_decode($amount); 

Instead of a space you get a character ? . So use str_replace again to remove the character ? :

 $result =str_replace("?","", $result_amt ); 
0
source

Source: https://habr.com/ru/post/908179/


All Articles