I’m not sure, but it looks like there is a problem with the regular expression in the preg_replace statement. Try:
$excerpt = preg_replace( '/\[.*\]/', '', $excerpt );
The best way to work things like this out is to do your development in a local development environment and figure out how to log the output so you can see what’s going on. (You can still connect to a remote Discourse forum. Just publish your posts to a private category.)
Getting too involved with basic PHP programming is probably outside of the scope of this forum. If you’re really stuck, send me a message and I’ll see if I can help.