1

I'm working on a PHP file which can echo a price (given by an array from another website) converted in a certain currency (let's say in € for example).

I thought about using the in_array function to check if the price retreived from the other webpage contains the currency character or not. Only problem: it doesn't recognize that symbol, even if I use a predefined array. If I try to change the $currency array to the actual price given by the priceclass, it will work. Thanks in advance!

$html=file_get_contents('http://thewebsite');
preg_match_all("thepriceclass", $html, $a);

foreach(end($a) as $key=> $value)
{
   print print_r($value, true).'<br>';
   $currency = array("€");
   $value = str_split($value);
   $message = "This listing =/= €";

   foreach($value as $letter)
   {
      if (in_array($letter, $currency)) 
      {
         $message = "This listing == €";
         break;
      }
   }
   echo $message;
   break;
}
hakre
  • 193,403
  • 52
  • 435
  • 836
Speed
  • 21
  • 1
  • 2
  • 2
    Could be a problem with encodings. – Barmar Jun 22 '13 at 16:05
  • BTW, if you want to look for a character or string in another string, use `strstr()`, you don't need to split it into an array. – Barmar Jun 22 '13 at 16:07
  • try specifying the encoding as `unicode` , for example. http://en.wikipedia.org/wiki/Unicode – Kevin Jun 22 '13 at 16:23
  • @kevin: do you mean by using their unicode value as an array? – Speed Jun 23 '13 at 12:50
  • I haven't worked with such currency symbols before. But, I think this may help you http://php.net/manual/en/class.numberformatter.php – Kevin Jun 23 '13 at 16:57

1 Answers1

0

How many currencies do you have? It it's not all of them, why not use ISO codes (ex. USD, EUR, JPY etc.) instead?

I would create a little function that converts symbols into code and vice versa.

EDIT:

In the comments you said you are working with €, $, £ and pуб.

I think the problem is the encoding, either because PHP is latin1, or things like that.

I'd just use the currency code (http://en.wikipedia.org/wiki/ISO_4217). In you case: USD, EUR, GBP, RUB, and compare with that.

I don't understand your code fully, as you look for the value in the array, but the you always print €. I assume it's just for testing.

I'd do:

$currency = array('EUR');

or:

$currency = array('USD', 'EUR', 'GBP', 'RUB');

If you can't use the above I'd do something like this...

$currencies = array('USD'=>'$', 'EUR'=> '€', 'GBP'=>'£', 'RUB'=>'pуб');

...and use the array values and keys to compare/print.

Would that work?

nkkollaw
  • 1,947
  • 1
  • 19
  • 29
  • Ah, well I would definitely do that, then. I'll edit my answer. – nkkollaw Jun 23 '13 at 13:59
  • It still doesn't work even if I use the Unicode instead of the $,€,... symbol. Could this be because of the padder of the price (a dollar price is for example $0.50 whereas a euro price is 0.50€ for example)? – Speed Jun 23 '13 at 14:24
  • OMG... I would think so, but from your code I can't really tell what's going on. – nkkollaw Jun 23 '13 at 19:31