dwest Posted March 14, 2008 Share Posted March 14, 2008 Hi, Many thanks to member sasa who provided me with the code below last night under a previous topic. I thought it was all buttoned up until I noticed this morning how eBay was providing the data. The code formats a response I receive from the eBay API. The data represents the time left in an auction and is typically supplied as: P2T9H37M8S Broken down, that is: P(period)=2days T=9Hours37Minutes8Seconds I thought all was well with the code except I've discovered that eBay supplies the data a bit differently than I thought as the time winds down. Thus the code breaks. It seems when the hours reach zero, eBay then supplies the data like this: PT37M8S And when Minutes reaches zero, like this: PT8S Sooooo... How can the code be modified to accommodate this? $timeLeft = $res->Item->TimeLeft; $x =array(1 => 'Day', 'Hour', 'Minute'); preg_match('/P(\d*)T(\d*)H(\d*)M/',$timeLeft, $d); for ($i = 1; $i < 4; $i++){ $dare .= $d[$i] ? $d[$i].' '.$x[$i].($d[$i] > 1 ? 's ' : ' ') :''; } Any help would be greatly appreciated! Quote Link to comment Share on other sites More sharing options...
effigy Posted March 14, 2008 Share Posted March 14, 2008 / \A P(\d*) T (?\d*)H)? (?\d*)M)? (\d*)S \z /x Quote Link to comment Share on other sites More sharing options...
dwest Posted March 14, 2008 Author Share Posted March 14, 2008 Thanks effigy! Please pardon my regex ignorance but is that meant to replace the entire snippet? Quote Link to comment Share on other sites More sharing options...
effigy Posted March 14, 2008 Share Posted March 14, 2008 Yes. Quote Link to comment Share on other sites More sharing options...
dwest Posted March 14, 2008 Author Share Posted March 14, 2008 Thanks! Actually my definition of snippet was the entire piece of code I figured you meant the regex string though so I tried it and of course it worked Thanks Much! One other tiny thing...the php part omits the display of seconds once minutes are zero. What do I need to make seconds display as well? Quote Link to comment Share on other sites More sharing options...
effigy Posted March 14, 2008 Share Posted March 14, 2008 <pre> <?php $tests = array( 'P2T9H37M8S', 'PT37M8S', 'PT8S', ); $pieces = array( 'days', 'hours', 'minutes', 'seconds' ); foreach ($tests as $test) { echo "<hr/><b>$test</b><br/>"; preg_match('/ \A P(\d*) T (?\d*)H)? (?\d*)M)? (\d*)S \z /x', $test, $matches); ### Toss full match. array_shift($matches); ### Show matches. print_r($matches); ### Create display string. $display = ''; $index = 0; foreach ($matches as $match) { if ($match !== '') { $display .= $match . ' ' . $pieces[$index] . ', '; } ++$index; } $display = trim($display, ', '); echo $display; } ?> </pre> Quote Link to comment Share on other sites More sharing options...
dwest Posted March 14, 2008 Author Share Posted March 14, 2008 HeHe...THAT is meant to replace the whole snippet, yes? Thanks a million for your input on this Since what I have now is this: $timeLeft = $res->Item->TimeLeft; $x =array(1 => 'Day', 'Hour', 'Minute'); //if using as quicktag in wordpress you must escape the backslashes because the quicktag insertion removes them. preg_match('/\\AP(\\d*)T(?\\d*)H)?(?\\d*)M)?(\\d*)S\\z/x',$timeLeft, $d); //preg_match('/\AP(\d*)T(?\d*)H)?(?\d*)M)?(\d*)S\z/x',$timeLeft, $d); //original code for ($i = 1; $i < 4; $i++){ $dare .= $d[$i] ? $d[$i].' '.$x[$i].($d[$i] > 1 ? 's ' : ' ') :''; } Could it not be tweaked to add the display of seconds? Is there something about this code that is faulty? Quote Link to comment Share on other sites More sharing options...
effigy Posted March 14, 2008 Share Posted March 14, 2008 Change $i < 4 to $i < 5. Quote Link to comment Share on other sites More sharing options...
dwest Posted March 14, 2008 Author Share Posted March 14, 2008 Geeez! I wish I knew this stuff. I simply don't use it enough to warrant the education. I'm an artist getting my own site online so painting is what I do. I used to develop sites but never got into regexp or much scripting. The sites I built were pretty simple. All in WordPress. Thanks so much for your help on this. I can't imagine how folk like me would get along without your help and those like you. You are all much appreciated! 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.