Jump to content

regex guru needed to tweak this snippet please


dwest

Recommended Posts

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!  :)

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

<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> 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.