Working with 'eregi_replace'

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Khrysller
Forum Newbie
Posts: 5
Joined: Wed Jan 19, 2005 12:41 pm

Working with 'eregi_replace'

Post by Khrysller »

Hi PHPers,

I am new in PHP program stuff, and I discover a great thing called 'eregi_replace', it is helping me work with SNMP stuff.

So my scene is:

Code: Select all

$tempo_de_bateriaget= snmpget("$host", $community, ".1.3.6.1.4.1.318.1.1.1.2.2.3.0");
all the variables are working fine, and the result is:

Timeticks: (102000) 0:17:00.00

so I use the code:

Code: Select all

$tempo_de_bateria = eregi_replace("Timeticks: (102000)","",$tempo_de_bateriaget);
but it doesn´t work (any information are showed)

and when I do:

Code: Select all

$tempo_de_bateria = eregi_replace("Timeticks:","",$tempo_de_bateriaget);
the result: (102000) 0:17:00.00

Question:

There´s a way to show just: 17:00.00 ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

parens are metacharacters in regular expressions. You will need to escape them... although the way you are using it.. you can just use str_replace, which will be faster.
Khrysller
Forum Newbie
Posts: 5
Joined: Wed Jan 19, 2005 12:41 pm

Post by Khrysller »

Mmmmmmmm nice, thanks for the reply ; )

I was just thinking... the value 102000 will chance if 0:17:00.00 change too... so.. there´s a way to cut everthing that comes into the "()" ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$newstring = preg_replace('#^.*?\)\s*(.*?)$#s', '', $yourtext);
Khrysller
Forum Newbie
Posts: 5
Joined: Wed Jan 19, 2005 12:41 pm

Post by Khrysller »

Mmmm this code didn´t show anything... I was searching around and found some code examples.. so I am trying do adapt...

Code: Select all

$tempo_de_bateriaget= snmpget("$host", $community, ".1.3.6.1.4.1.318.1.1.1.2.2.3.0");
$tempo_de_bateria = preg_replace('/(ї0-9])/e', '', $tempo_de_bateriaget);
this results: Timeticks: () ::.

There´s a way to tell to just eliminate the timestick word and what is into the brackets, inclunding the brackets?

I am trying to understand some of the examples here: http://br2.php.net/preg_replace

thanks in advance : )
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

sorry.. forgot to tell it what it needed to do:

Code: Select all

<?php

	$text = 'Timeticks: (102000) 0:17:00.00';
	
	$newstring = preg_replace('#^.*?\)\s*(.*?)$#s', '\\1', $text);
	
	echo $text . "\n";
	echo $newstring;

?>

Code: Select all

$newstring = preg_replace('#^.*?\)\s*#s', '', $text);
also works.
Khrysller
Forum Newbie
Posts: 5
Joined: Wed Jan 19, 2005 12:41 pm

Post by Khrysller »

WOW
amazing ^^

i can´t compile anything of this code just looking at it, heheheheh

feyd, I use the code:

Code: Select all

$newstring = preg_replace('#^.*?\)\s*#s', '', $text);
and it´s bring me what I want, u rock dude!

I was just thinking if u could add some more code to eliminate the (in bold):

4:10:00.00

don´t wanna disturb,...
I am so happy you helping me A LOT, believe ^^

triple thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$newstring = preg_replace('#^.*?\)\s*(\d+:\d+).*?$#s', '\\1', $text);
Khrysller
Forum Newbie
Posts: 5
Joined: Wed Jan 19, 2005 12:41 pm

Post by Khrysller »

Amazing dude ^^
thanks a lot for your help : )
Post Reply