views:

73

answers:

2

I have a text string that contain some references numbers like [3][2][4] or [1] or [5][7]

can you please help me removing all numbers between brackets ex. [4] from that string

using regular expressions Regex ?

Thanks

+6  A: 

Just do a preg replace on /\[\d\]/ and replace it with ''

$s = preg_replace( '#\[\d+\]#', '', $context );

I believe that will kill multiple occurrences.

meder
+1 but use `\d+`, there might be 2 or 3 digit numbers
Diadistis
He didn't really specify but sure.
meder
While it would be more typing, you'll have less runtime overhead by using `str_replace`. I'd go with meder, he isn't expecting more than one number contained in brackets.
Nerdling
+1  A: 

Since the question asks to remove numbers between the brackets and not the brackets themselves, I think this would be what he is looking for:

$string = preg_replace('/(?<=\[)\d+(?=\])/', '', $string);
tsgrasser