0

I'm trying to access

1.20163

from the following:

1 British Pound Sterling = 1.20163 Euro

So far I have:

$exchange['rate'] = $xml->channel->item[15]->description;
$rate = preg_match('/^[0-9]{1,}$/', '', $exchange['rate']);

However, this seems to returns

1 British Pound Sterling = 1.20163 Euro

Any ideas?

newToJava
  • 173
  • 1
  • 15
  • You're also confusing the parameters of [`preg_match`](http://php.net/preg_match) with [`preg_replace`](http://php.net/preg_replace). – mario Feb 01 '12 at 16:56
  • *(related)* [How to implement Exchange rates in PHP](http://stackoverflow.com/questions/1973569/how-to-implement-exchange-rate-in-php/1973823#1973823) – Gordon Feb 01 '12 at 17:17

3 Answers3

3

I think you using preg_match wrong You want to extract the value of 1.20163 from the string, right? Then do this:

$s = '1 British Pound Sterling = 1.20163 Euro';
preg_match('/([0-9]+\.[0-9]+)/', $s, $matches);
$result = $matches[0];

You got your result in $result.

Dmitri Snytkine
  • 1,096
  • 1
  • 8
  • 14
0
/^[0-9]{1,}$/

means you want to match a line containing only numbers. You wont get any match in your case. You also use preg_match in a wrong way. See the documentation.

Try something like:

$exchange['rate'] = $xml->channel->item[15]->description;
preg_match('/=\s*([0-9.]+)\s/', $exchange['rate'], $matches);
// the result will be in $matches
$rate = $matches[1];
morja
  • 8,297
  • 2
  • 39
  • 59
0

Try

'/([0-9]{1,}\.[0-9]{1,}) /'

Match numbers then decimal then numbers then space.

James L.
  • 4,032
  • 1
  • 15
  • 15