robbyd Posted March 5, 2008 Share Posted March 5, 2008 Im trying to find a way to search and replace multiple instances in a string For example if I have string like: $string = '[this] is a [example] string [end]'; and function like: preg_replace ( '#\[(.+?)\]#mi','',$string); The output I would like is: "is a string" However currently it outputs blank, because it takes the first [ and the last ] and replaces it with blank instead of each instance of [ ]. Any suggestions or ideas on what to change would be helpful. Quote Link to comment Share on other sites More sharing options...
c4onastick Posted March 6, 2008 Share Posted March 6, 2008 Try to stay away from the these: .+? or .*? Instead, use something more specific (and greedy) like this: $string = '[this] is a [example] string [end]'; echo preg_replace('/\[[^\]]+\]/', '', $string); Cheers! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.