Forum Moderators: coopster
$teststr = 'This is a {b}test {/b} of {i}preg{/i} stuff'; // These {} should be brackets, forum software would strip them out otherwise.echo preg_replace('(\[.*\])', "", $teststr);
I just want the '[whatever]', whatever is in brackets to go away. Here's what I get running the above:
This is a stuff
I thought I was matching a [ then 0 or more characters then a closing ]. Obviously I'm missing something. Appreciate any assistance.
baze
echo preg_replace('/\[\/?.*?\]/', "", $teststr);
The .*? means 'non-greedy', which means it will stop at the next end bracket. If you do just .*, it will go all the way until the last end bracket in the entire string, which woudn't be good if you had more than one bracket enclosed item. Adding \/? in there also makes it so it will get rid of the closing tags.
Another option would be to simply add the Ungreedy [php.net] modifier
echo preg_replace('(\[.*\])Us', "", $teststr);I threw in the "s" modifier too, in case the pattern ever broke over a newline.