Scooby08 Posted December 18, 2009 Share Posted December 18, 2009 I'm trying to figure out a way to split up these lines: +7.5-110 -7.5-110 +7.5+110 -7.5+110 Shooting for a way to get this: +7.5 (-110) -7.5 (-110) +7.5 (+110) -7.5 (+110) I was thinking the the split function might be able to get close, but not quite sure how to make that work because I don't know how to make it split at a specific delimiter... Thanks guys.. Link to comment https://forums.phpfreaks.com/topic/185650-how-to-split-these-up/ Share on other sites More sharing options...
akitchin Posted December 18, 2009 Share Posted December 18, 2009 it looks like the string's format is very standardized - you could just use substr if that's the case. otherwise, what sort of formats are you expecting? Link to comment https://forums.phpfreaks.com/topic/185650-how-to-split-these-up/#findComment-980316 Share on other sites More sharing options...
salathe Posted December 18, 2009 Share Posted December 18, 2009 As akitchin said, the example seems pretty consistent (plus/minus followed by 110). If that's always the case then some simple string manipulation might suffice. If not, and something like a regular expression is necessary then the following example snippet takes your input string and outputs what you're shooting for. $subject = '+7.5-110 -7.5-110 +7.5+110 -7.5+110'; echo preg_replace('/[+-]\d+$/m', ' ($0)', $subject); Link to comment https://forums.phpfreaks.com/topic/185650-how-to-split-these-up/#findComment-980319 Share on other sites More sharing options...
Scooby08 Posted December 18, 2009 Author Share Posted December 18, 2009 Well it could be any of these: +7.5-110 -13.5-110 +24.5+310 -9.5+550 +7-1000 -7+5000 where as I'd want: +7.5-110 = +7.5 (-110) -13.5-110 = -13.5 (-110) +24.5+310 = +24.5 (+310) -9.5+550 = -9.5 (+550) +7-1000 = +7 (-1000) -7+5000 = -7 (+5000) The numbers and symbols are never consistent per game (all games have different odds).. they are only consistent as to the format of the original string.. Am gonna try out salathe's suggestion quick.. Link to comment https://forums.phpfreaks.com/topic/185650-how-to-split-these-up/#findComment-980325 Share on other sites More sharing options...
Scooby08 Posted December 19, 2009 Author Share Posted December 19, 2009 Awesome! As far as I can see it's working perfectly! Thank you salathe.. You got one more in ya?? hah Say I got these couple of possible strings: o134.5-110 u134.5-110 o9.5-310 u9.5+230 By using something similar to what you just did, how could I make those like so (need 2 ways): 1st way: o134.5-110 = 134.5 u134.5-110 = 134.5 o9.5-310 = 9.5 u9.5+230 = 9.5 2nd way: o134.5-110 = o (-110) u134.5-110 = u (-110) o9.5-310 = o (-310) u9.5+230 = u (+230) Thank you so very much in advanced!! Link to comment https://forums.phpfreaks.com/topic/185650-how-to-split-these-up/#findComment-980341 Share on other sites More sharing options...
salathe Posted December 19, 2009 Share Posted December 19, 2009 Sure, but make sure you really understand what is going on. $subject = 'o134.5-110 u134.5-110 o9.5-310 u9.5+230'; // Same pattern for both ways $pattern = '/^([ou])(\d+(?:\.\d)?)([+-]\d+)$/m'; // Way 1 echo preg_replace($pattern, '$2', $subject); // Way 2 echo preg_replace($pattern, '$1 ($3)', $subject); Link to comment https://forums.phpfreaks.com/topic/185650-how-to-split-these-up/#findComment-980345 Share on other sites More sharing options...
Scooby08 Posted December 19, 2009 Author Share Posted December 19, 2009 For sure! I'm looking up documentation on what you did right now so I can see what all that means.. Thanks a million!! Link to comment https://forums.phpfreaks.com/topic/185650-how-to-split-these-up/#findComment-980354 Share on other sites More sharing options...
salathe Posted December 19, 2009 Share Posted December 19, 2009 Any questions, do feel free to ask here. I'm off for a snooze, but some kind soul will be available to asnwer your questions (or myself in the morning). Link to comment https://forums.phpfreaks.com/topic/185650-how-to-split-these-up/#findComment-980355 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.