Jump to content

Regular Expression


jester18

Recommended Posts

Hello All,

 

I am wondering if you could help me? I know I have to use a regular expression but I'm really not sure how to achieve this, I want to be able to pull only the rate out of numerous currencies, 

 

an example: 1 British Pound Sterling = 1.55727 Canadian Dollar

 

all I want to pull from that is 1.55727  ...the only way I know how is to do it by the amount of digits after the equals sign however that number can change so now im stuck.

 

Would anyone be able to help me?

Link to comment
https://forums.phpfreaks.com/topic/194033-regular-expression/
Share on other sites

$regex = "/= \d+\.?\d+/";
$test = "1 British Pound Sterling = 1.55727 Canadian Dollar";
$matches = preg_match($regex, $test, $output);
echo $output[0];

 

That will get the "= 1.55727" portion

 

the regex just says = followed by a space, followed by any digits, followed by an optional decimal, followed by and number of digits

 

Hope that helps

Link to comment
https://forums.phpfreaks.com/topic/194033-regular-expression/#findComment-1021033
Share on other sites

I thought about that... I left them in so it wouldn't get confused by any other numbers in the string (if there happen to be more than just the 1 at the beginning). You could just do it with

 

$output[0] = str_replace("= ", "", $output[0]);

 

If though it will always be "1 thiscurrency = numeric rate" you could just remove the "= " from the regex so it would be

 

$regex = "/\d+\.?\d+/";

Link to comment
https://forums.phpfreaks.com/topic/194033-regular-expression/#findComment-1021057
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.