473,378 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

text formatting using eregi_replace

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

"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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Fred | last post by:
Hi, With the sentence : "Bordeaux est au bord de l'eau" How to do to underline, for instance, the word "eau" ? without underlining the substring of "Bordeaux" ? I don't know how to isolate...
1
by: fartsniff | last post by:
Hello all. I am starting to work on a URL "cleaner" of sorts. The code below is only checking for a few simple entries on the URL, but for some reason it is not replacing them with "" when...
2
by: SK | last post by:
Is there a way to store HTML into a MySQL TEXT column, yet be able to search over textual content only? For example, if I store the following HTML snippet: <p>A very <em>small</em>...
2
by: saiena | last post by:
Is there a way in my regular expression syntax to cause alternating occurences of the search string to be replaced? Here's my code: $item = eregi_replace($search_string, $replace_string,...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
3
by: michael sorens | last post by:
The documentation for the RichTextBox is sketchy at best. I want to do a very simple task but I cannot find information on this. I am using a RichTextBox as an output window. Some text I want to...
24
by: =?Utf-8?B?RHVja3dvbg==?= | last post by:
Hello, My code is suppose to write to an existing file. But after my C# code appends the file, the previous text loses all the endline characters, becoming one long line. How can I retain the...
16
by: Neil | last post by:
I posted a few days ago that it seems to me that the Access 2007 rich text feature does not support: a) full text justification; b) programmatic manipulation. I was hoping that someone might...
9
by: Bob | last post by:
Hi, Maybe this problem has already been mentionned, but I am new to this group, so here we go. $string = "This is for ABC only" $new = (eregi_replace('ABC','<b>ABC</b>',$string)); print...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.