By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,401 Members | 814 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,401 IT Pros & Developers. It's quick & easy.

text formatting using eregi_replace

P: n/a
Hi,

I've had a search through google but couldn't really find the answer I was
looking for.I'm
new to PHP, so please take it <relatively> easy.

I've created a script which runs some SNMP queries. I'd like help if
possible in formatting one of the
values it returns.

When you run a query for the system uptime the result is displayed like so:

Timeticks: (4592345) 12:45:23.45

1.) I would like to remove the first two "fields" so I am left with:
12:45:23.45

I have managed to remove the Timeticks: part but don't know how to remove
the (4592345) (because this is of variable size).

$sysuptime = eregi_replace("Timeticks: ","",$sysuptime);

I don't know if it is possible but could you say remove everything within
the brackets - is that possible in a regular expression?
$sysuptime = eregi_replace("[()]","",$sysuptime); ???? (I tried that and it
didn't work ;) )

2.) If possible I would like to convert the output 12:45:23.45 to something
more like 12hr 45 mins

Obviously as the uptime increases that needs to incorporate _days_ as well.

3. Is it good/bad practice to use the same variable name twice in the
statement above?

Many thanks

David
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
David wrote:
When you run a query for the system uptime the result is displayed like so:

Timeticks: (4592345) 12:45:23.45

1.) I would like to remove the first two "fields" so I am left with:
12:45:23.45

I have managed to remove the Timeticks: part but don't know how to remove
the (4592345) (because this is of variable size).

$sysuptime = eregi_replace("Timeticks: ","",$sysuptime);

I don't know if it is possible but could you say remove everything within
the brackets - is that possible in a regular expression?
switch to preg_* (info @ http://www.php.net/PCRE)
these functions (supposedly) are faster and more powerful

$sysuptime = preg_replace('@Timeticks: \(\d+\) @', '', $sysuptime);

comments:
@ start the regex (you can use other characters)
Timeticks: and a space are matched verbatim
\( one verbatim "("
\d+ one or more digits
\) verbatim ")"
space another space
@ end the regex

the parentheses had to be escaped (with the backslash character) because
they have a special meaning in regular expressions.

2.) If possible I would like to convert the output 12:45:23.45 to something
more like 12hr 45 mins
For just the hours example I'd probably use substr():
$sysuptime = substr($sysuptime, 0, 2) . 'hr '
. substr($sysuptime, 3, 2) . ' mins';
Obviously as the uptime increases that needs to incorporate _days_ as well.
Well ... maybe a regexp is better :-)
Have a go at it. If you have problems report back here.

3. Is it good/bad practice to use the same variable name twice in the
statement above?


I use it all the time (when that is what I want, of course).
If it weren't ok to use they would never have invented the ++ or --
things!

$a++; // short way to use the same var on both sides of the = sign :)
HTH
--
--= my mail address only accepts =--
--= Content-Type: text/plain =--
--= Size below 10K =--
Jul 17 '05 #2

P: n/a

"Pedro Graca" <he****@hotpop.com> wrote in message
news:bq*************@ID-203069.news.uni-berlin.de...
David wrote:
When you run a query for the system uptime the result is displayed like so:
Timeticks: (4592345) 12:45:23.45

1.) I would like to remove the first two "fields" so I am left with:
12:45:23.45

I have managed to remove the Timeticks: part but don't know how to remove the (4592345) (because this is of variable size).

$sysuptime = eregi_replace("Timeticks: ","",$sysuptime);

I don't know if it is possible but could you say remove everything within the brackets - is that possible in a regular expression?
switch to preg_* (info @ http://www.php.net/PCRE)
these functions (supposedly) are faster and more powerful

$sysuptime = preg_replace('@Timeticks: \(\d+\) @', '', $sysuptime);

comments:
@ start the regex (you can use other characters)
Timeticks: and a space are matched verbatim
\( one verbatim "("
\d+ one or more digits
\) verbatim ")"
space another space
@ end the regex

the parentheses had to be escaped (with the backslash character) because
they have a special meaning in regular expressions.

2.) If possible I would like to convert the output 12:45:23.45 to something more like 12hr 45 mins


For just the hours example I'd probably use substr():
$sysuptime = substr($sysuptime, 0, 2) . 'hr '
. substr($sysuptime, 3, 2) . ' mins';
Obviously as the uptime increases that needs to incorporate _days_ as

well.
Well ... maybe a regexp is better :-)
Have a go at it. If you have problems report back here.

3. Is it good/bad practice to use the same variable name twice in the
statement above?


I use it all the time (when that is what I want, of course).
If it weren't ok to use they would never have invented the ++ or --
things!

$a++; // short way to use the same var on both sides of the = sign :)
HTH
--
--= my mail address only accepts =--
--= Content-Type: text/plain =--
--= Size below 10K =--


Pedro,

Thank you for the help. I understand the regexp you used in 1.) above.
Excellent explanation.

I will certainly try and workout how to do 2.)

:)

David
Jul 17 '05 #3

P: n/a

"David" <ja********@dontspamme.ntlworld.com> wrote in message
news:3f*********************@mercury.nildram.net.. .

"Pedro Graca" <he****@hotpop.com> wrote in message
news:bq*************@ID-203069.news.uni-berlin.de...
David wrote:
When you run a query for the system uptime the result is displayed
like
so:
Timeticks: (4592345) 12:45:23.45

1.) I would like to remove the first two "fields" so I am left with:
12:45:23.45

I have managed to remove the Timeticks: part but don't know how to remove the (4592345) (because this is of variable size).

$sysuptime = eregi_replace("Timeticks: ","",$sysuptime);

I don't know if it is possible but could you say remove everything within the brackets - is that possible in a regular expression?


switch to preg_* (info @ http://www.php.net/PCRE)
these functions (supposedly) are faster and more powerful

$sysuptime = preg_replace('@Timeticks: \(\d+\) @', '', $sysuptime);

comments:
@ start the regex (you can use other characters)
Timeticks: and a space are matched verbatim
\( one verbatim "("
\d+ one or more digits
\) verbatim ")"
space another space
@ end the regex

the parentheses had to be escaped (with the backslash character) because
they have a special meaning in regular expressions.

2.) If possible I would like to convert the output 12:45:23.45 to something more like 12hr 45 mins


For just the hours example I'd probably use substr():
$sysuptime = substr($sysuptime, 0, 2) . 'hr '
. substr($sysuptime, 3, 2) . ' mins';
Obviously as the uptime increases that needs to incorporate _days_ as

well.

Well ... maybe a regexp is better :-)
Have a go at it. If you have problems report back here.

3. Is it good/bad practice to use the same variable name twice in the
statement above?


I use it all the time (when that is what I want, of course).
If it weren't ok to use they would never have invented the ++ or --
things!

$a++; // short way to use the same var on both sides of the = sign :)
HTH
--
--= my mail address only accepts =--
--= Content-Type: text/plain =--
--= Size below 10K =--


Pedro,

Thank you for the help. I understand the regexp you used in 1.) above.
Excellent explanation.

I will certainly try and workout how to do 2.)

:)

David

Well, I've found another SNMP command which has possibly made things easier.

Anyway, the uptime is now displayed thus:

0:13:22:12.90

(0 days, 13 hours, 22 mins, 19 secs, 90 ticks (I think that's the word
that's used))

Anyway, I am having great trouble in converting the above using a regexp to
the format:

X days, Y hours, Z minutes (not worried about the seconds and 1/100's)

I have tried searching google but I don't really know what I am searching
for.

Anyway, if someone could advise me of what I should be searching for or
could offer some advice I'd be most grateful.
My current thinking of how I should convert between the formats above would
be:

1. search for FIRST instance of ":" then replace with " days "
2. search for FIRST instance again of ":" in the updated variable then
replace with " minutes ".
etc ...

I don't know if that makes sense to anyone else and is a good idea?

What I'm really asking is how can you construct a regexp so that it looks
for only the first instance of a character, then replaces it and only it.

(I tried
preg_replace = ('@:@', '' days ", $sysuptime); // and of course it
replaced all instances of ":" with "days"

Regards,

David
Jul 17 '05 #4

P: n/a
David wrote:
the uptime is now displayed thus:

0:13:22:12.90

(0 days, 13 hours, 22 mins, 19 secs, 90 ticks (I think that's the word
that's used))
I think your best option is to use explode().

<?php
$data = '0:13:22:12.90';
$parts = explode(':', $data);
// $parts[0] = '0'; $parts[1] = '13'; $parts[2] = '22'; $parts[3] = '12.90'

echo $parts[0], ' days, ', $parts[1], ' hours, ', $parts[2], ' minutes.';
?>

Anyway, I am having great trouble in converting the above using a regexp to
the format:

X days, Y hours, Z minutes (not worried about the seconds and 1/100's)

I have tried searching google but I don't really know what I am searching
for.
Don't google for regexps -- try them!
http://www.weitz.de/regex-coach/

My current thinking of how I should convert between the formats above would
be:

1. search for FIRST instance of ":" then replace with " days "
2. search for FIRST instance again of ":" in the updated variable then
replace with " minutes ".
etc ...

I don't know if that makes sense to anyone else and is a good idea?
Why do it in steps when you can do it all at once? (see below)

What I'm really asking is how can you construct a regexp so that it looks
for only the first instance of a character, then replaces it and only it.


preg_replace() has a fourth parameter that specifies how many
replacements you want to make
http://www.php.net/preg_replace
But you don't want to solve your current problem using this :)
I would not use preg_replace() for this problem, but if I must use
preg_replace(), this is how I'd do it:

<?php
$data = '0:13:22:12.90';

$regexp = '/^(\d+):(\d{2}):(\d{2}).*$/';
$subst = '$1 days, $2 hours, $3 minutes';

$time = preg_replace($regexp, $subst, $data);
echo $time;
?>
$regexp comments:
/ ## start regexp
^ ## match null string at start of data
(\d+) ## GRAB one or more digits into $1
: ## verbatim
(\d{2}) ## GRAB _exactly_ 2 digits into $2
: ## verbatim
(\d{2}) ## GRAV -exactly_ 2 digits into $3
..* ## match any sequence of characters (up to)
$ ## the null string at end of data
/ ## end regexp

$subst comments:
the references to the GRABbed thing from the regexp ($1, $2, and $3)
are 'replaced' by themselves;
then the whole string is replaced by $subst
Happy Coding :)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.