Regular expressions replace

51 Views Asked by At

I need to remove

[0037][user name] 

combination from a sentence. In the first brackets always containing numbers eg:

[0032]

Digit count will not exceed than 4 by any chance. In the second brackets always containing letters eg:

[first name]

anyone have an idea how to do this?

2

There are 2 best solutions below

0
On BEST ANSWER

You can use preg_replace() to implement regular expression syntax and try the following expression.

$str = preg_replace('/\[\d+]\[[a-z ]+]/i', '', $str);
2
On
\[\d{1,4}\]\[[a-zA-Z ]+\]

This should do it.Replace by empty string.See demo.

http://regex101.com/r/oE6jJ1/22

$re = "/\\[\\d{1,4}\\]\\[[a-zA-Z ]+\\]/im";
$str = "asdas asdsad [1234][asd asd] asdasd";
$subst = "";

$result = preg_replace($re, $subst, $str);