Forum Moderators: coopster

Message Too Old, No Replies

Regex eregi problem

         

rfontaine

7:40 pm on Mar 2, 2006 (gmt 0)

10+ Year Member



I have a bunch of text with numbered lines that look like this:

[11] blah blah blah
[12] more text
[13] and even more text
[14] etc

I am trying to make a regular expression that gets rid of
all the numbers and their brackets.

I came up with something like this but the problem is getting rid of the brackets correctly:

$the_text = ereg_replace("[\[0-9\]]", "", $the_text);

Any ideas what is wrong?

Thank you in advance :-)

rfontaine

7:47 pm on Mar 2, 2006 (gmt 0)

10+ Year Member



anyone?

coopster

8:09 pm on Mar 2, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$string = preg_replace("/\[[0-9]+\]/", '', $string);

The [0-9] is what is referred to as a character class so it has to be enclosed in brackets. The plus (+) sign says to match one or more of them.

rfontaine

8:19 pm on Mar 2, 2006 (gmt 0)

10+ Year Member



Excellent Coopster! It works :-)

xfinx

8:32 am on Mar 3, 2006 (gmt 0)

10+ Year Member



Better use perl ;-)

$the_text =~ s/[\[0-9\]]//;

:P

coopster

11:11 pm on Mar 3, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



preg_replace is a Perl-Compatible Regular Expression Function [php.net].

However, if you want to use perl you still have to get the regular expression correct ;)

$string =~ s/\[[0-9]+\]//g;