Page 1 of 1
Working with 'eregi_replace'
Posted: Wed Jan 19, 2005 12:55 pm
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 ?
Posted: Wed Jan 19, 2005 1:16 pm
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.
Posted: Thu Jan 20, 2005 7:03 am
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 "()" ?
Posted: Thu Jan 20, 2005 9:02 am
by feyd
Code: Select all
$newstring = preg_replace('#^.*?\)\s*(.*?)$#s', '', $yourtext);
Posted: Thu Jan 20, 2005 10:22 am
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 : )
Posted: Thu Jan 20, 2005 10:26 am
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.
Posted: Thu Jan 20, 2005 10:47 am
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
Posted: Thu Jan 20, 2005 10:52 am
by feyd
Code: Select all
$newstring = preg_replace('#^.*?\)\s*(\d+:\d+).*?$#s', '\\1', $text);
Posted: Thu Jan 20, 2005 11:43 am
by Khrysller
Amazing dude ^^
thanks a lot for your help : )