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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.