regex - PHP remove numbers in brackets -
i have string looks following in php: foo [123][bar]
, i'd remove [123]
string entirely end foo [bar]
.
another example blah [9484][m0200 blah blah blah]
makes things more complicated because can't remove numbers , remove empty brackets. first set of brackets contains 3-4 digit number, , never letters, second brackets can contain anything.
any suggestions how i'd go doing this? thanks.
this regex
\[\d{3,4}\]
will find 3-4 digit numbers inside brackets.
demo: https://regex101.com/r/kh3nz5/1
used preg_replace, http://php.net/manual/en/function.preg-replace.php, replace found instances.
echo preg_replace('~\[\d{3,4}\]~', '', 'blah [9484][m0200 blah blah blah]');
output:
blah [m0200 blah blah blah]
\d
single number , {3,4}
says @ least 3 occurrences of number , no more 4.
pages more detailed info on regexs,
http://www.rexegg.com/
http://www.regular-expressions.info/
Comments
Post a Comment