If you’d like to match [*]
with a regex, you have to escape all three characters with backslashes because they are meta-characters. Next, you have to find out if [*]
is immediately followed by a newline (\n
or \r\n
) or end of list ([/list]
). Then replace it with an empty string.
$body = "[list][*]Hydrogen
[*]
[*]Helium
[*]Lithium
[*]
[*]
[*]Beryllium
[*]Boron
[*]Carbon
[*]Nitrogen
[*]
[*]Oxygen
[*][/list]";
$body = preg_replace("/\[\*\](?:\r?\n|(?=\[\/list\]))/", "", $body);
print($body);
The output will be:
[list][*]Hydrogen
[*]Helium
[*]Lithium
[*]Beryllium
[*]Boron
[*]Carbon
[*]Nitrogen
[*]Oxygen
[/list]