jester18 Posted March 3, 2010 Share Posted March 3, 2010 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? Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted March 3, 2010 Share Posted March 3, 2010 $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 Quote Link to comment Share on other sites More sharing options...
jester18 Posted March 3, 2010 Author Share Posted March 3, 2010 thanks for the swift reply, do you know how i could remove the ='s aswell from it? so its literally just the numerical portion 1.55727 Thank you! Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted March 3, 2010 Share Posted March 3, 2010 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+/"; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.