doh85 Posted November 15, 2010 Share Posted November 15, 2010 I am gathering some data from a website to do with currencies. I have managed to strip out the rates into this format. price['AFN:CUR'] = 45.17 The rates are all structured like this, however i want them to be structured like this, but stored in 2 separate variables. So for example: echo $country_code[$i]; // AFN echo $rate[$i]; // 45.17 AFN 45.17 Any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/218746-reg-ex-help/ Share on other sites More sharing options...
JAY6390 Posted November 15, 2010 Share Posted November 15, 2010 ~price['([^:]+):CUR'] = ([\d\.]+)~ Match 1 would be the AFN, match 2 would be the amount Quote Link to comment https://forums.phpfreaks.com/topic/218746-reg-ex-help/#findComment-1134515 Share on other sites More sharing options...
doh85 Posted November 15, 2010 Author Share Posted November 15, 2010 Would it be possible to put this to use in an example please? Quote Link to comment https://forums.phpfreaks.com/topic/218746-reg-ex-help/#findComment-1134518 Share on other sites More sharing options...
JAY6390 Posted November 15, 2010 Share Posted November 15, 2010 If you show how you've got it to that stage, I'll put in an example Quote Link to comment https://forums.phpfreaks.com/topic/218746-reg-ex-help/#findComment-1134520 Share on other sites More sharing options...
doh85 Posted November 15, 2010 Author Share Posted November 15, 2010 PM'd you the code Quote Link to comment https://forums.phpfreaks.com/topic/218746-reg-ex-help/#findComment-1134525 Share on other sites More sharing options...
doh85 Posted November 15, 2010 Author Share Posted November 15, 2010 $content = file_get_contents(**URL**'); if ($content !== false) { $content = explode(";", $content); $count = count($content); for ($i = 1; $i < $count; ++$i) { echo $content[$i]; } } else { echo "Fail"; } Quote Link to comment https://forums.phpfreaks.com/topic/218746-reg-ex-help/#findComment-1134542 Share on other sites More sharing options...
JAY6390 Posted November 15, 2010 Share Posted November 15, 2010 After echo $content[$i]; use preg_match("~price['([^:]+):CUR'] = ([\d\.]+)~", $content[$i], $matches); $country_code[$i] = $matches[1]; $rate[$i] = $matches[2]; Quote Link to comment https://forums.phpfreaks.com/topic/218746-reg-ex-help/#findComment-1134545 Share on other sites More sharing options...
doh85 Posted November 15, 2010 Author Share Posted November 15, 2010 Warning: preg_match() [function.preg-match]: Compilation failed: unmatched parentheses at offset 13 in F:\xampp\htdocs\AWT\ass\conv.php on line 33 Line 33: preg_match("~price['([^:]+):CUR'] = ([\d\.]+)~", $content[$i], $matches); Quote Link to comment https://forums.phpfreaks.com/topic/218746-reg-ex-help/#findComment-1134547 Share on other sites More sharing options...
JAY6390 Posted November 15, 2010 Share Posted November 15, 2010 ah, you need a \ after the word "price" in the regex before the [ and it should work Quote Link to comment https://forums.phpfreaks.com/topic/218746-reg-ex-help/#findComment-1134548 Share on other sites More sharing options...
doh85 Posted November 15, 2010 Author Share Posted November 15, 2010 That has worked a treat, thank you very much. I have one more slight issue, "price['KPW:CUR'] = -99;" this one has not shown up. I have checked the original file to see. This currency is currently unavaliable. How would i go about editing the code to still pull the value of this even if the rate is -99? I have formatted the outputs to be displayed like so: echo $country_code[$i].' - '; echo $rate[$i].' <br/> '; At the very bottom its showing just a '-', how would i go about removing it, it must mean that the value is empty or something like that? Quote Link to comment https://forums.phpfreaks.com/topic/218746-reg-ex-help/#findComment-1134554 Share on other sites More sharing options...
JAY6390 Posted November 15, 2010 Share Posted November 15, 2010 Change ([\d\.]+)~ to (-?[\d\.]+)~ Quote Link to comment https://forums.phpfreaks.com/topic/218746-reg-ex-help/#findComment-1134556 Share on other sites More sharing options...
doh85 Posted November 15, 2010 Author Share Posted November 15, 2010 You have been a lifesaver. Cheers man. Any help with the final thing, the last dash right at the very bottom? Quote Link to comment https://forums.phpfreaks.com/topic/218746-reg-ex-help/#findComment-1134559 Share on other sites More sharing options...
JAY6390 Posted November 15, 2010 Share Posted November 15, 2010 it's likely to be an issue with an extra line. Just put array_pop($content); just before this line $count = count($content); which should do it Quote Link to comment https://forums.phpfreaks.com/topic/218746-reg-ex-help/#findComment-1134560 Share on other sites More sharing options...
doh85 Posted November 15, 2010 Author Share Posted November 15, 2010 Dude, thank you so much. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/218746-reg-ex-help/#findComment-1134561 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.