chiprivers Posted April 14, 2007 Share Posted April 14, 2007 Within my current project, I have come across a slight hurdle for which I believe regex is the solution. I am working with horse racing results and I am currently trying to breakdown a string containing the race length into seperate numeric fields for miles furlongs and yards. The string is presented int he format: $race_distance = "1m4f100y"; Obviously the m, f and y represent miles, furlongs and yards respectively. I want to end up with three strings like this: $miles = 1; $furlongs = 4; $yards = 100; Note however that the numeric figures may be of differeing digit length, not always as the example, and also the string may not include miles, furlongs and yards; it may be: $race_distance = "1m"; which would require: $miles = 1; $furlongs = 0; $yards = 0; Could anybody please help me with a small script that would create these results? Many thanks. Quote Link to comment Share on other sites More sharing options...
Guest prozente Posted April 14, 2007 Share Posted April 14, 2007 <pre><?php $data = '211m43f100y'; preg_match('/(?\d{0,})m)?(?\d{0,})f)?(?\d{0,})y)?/', $data, $matches); list($all, $miles, $furlongs, $yards) = $matches; echo 'miles: '.intval($miles)."\n". 'furlongs: '.intval($furlongs)."\n". 'yards: '.intval($yards)."\n"; ?></pre> EDIT: fixed pattern Quote Link to comment Share on other sites More sharing options...
chiprivers Posted April 14, 2007 Author Share Posted April 14, 2007 Perfect, thank you very much 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.