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. Link to comment https://forums.phpfreaks.com/topic/94598-string-replace-with-preg_replace/ 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! Link to comment https://forums.phpfreaks.com/topic/94598-string-replace-with-preg_replace/#findComment-484543 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.