andyd34 Posted October 12, 2009 Share Posted October 12, 2009 Ho wwould I remove square brackets and there contents from a string, have tried $option_title = preg_replace('`\[\w+\s+\w+\s+#\d+\]`', '', $option_title); and $option_title = preg_replace('/\[(^])*\)|]([])]/', '', $option_title); and $option_title = preg_replace('/\([^)]*\)|[()]/', '', $option_title); any suggestions Quote Link to comment Share on other sites More sharing options...
Alex Posted October 12, 2009 Share Posted October 12, 2009 try ~\[(.+?)\]~ Quote Link to comment Share on other sites More sharing options...
Adam Posted October 12, 2009 Share Posted October 12, 2009 Or even.. #\[.*\]# Quote Link to comment Share on other sites More sharing options...
.josh Posted October 12, 2009 Share Posted October 12, 2009 Or even.. #\[.*\]# Why don't you try running that through this string and see what happens: [blahblah] some random stuff [more blah] Quote Link to comment Share on other sites More sharing options...
Adam Posted October 12, 2009 Share Posted October 12, 2009 Darn it! #\[.*?\]# Quote Link to comment Share on other sites More sharing options...
cags Posted October 12, 2009 Share Posted October 12, 2009 While we're throwing out different methods... "~\[[^\]]*\]~" Quote Link to comment Share on other sites More sharing options...
Jenk Posted October 12, 2009 Share Posted October 12, 2009 /\[.*(?!<\])\]/mg Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted October 12, 2009 Share Posted October 12, 2009 Granted, these solutions will not take on nested square brackets (in the event the actual source string in question involves this). One possible solution could be: $str = 'This is a test to remove nested [[s]q[u]a[r]e] brackets [[from [t[hi]s] source [string]!]]'; echo $str = preg_replace('#\[[^[\]]*(??R)|.*?)+\]#', '', $str); EDIT - If the chunk of square brackets extends multiple lines, simply add the s modifier. @jenk, g is not a known modifier. Quote Link to comment Share on other sites More sharing options...
.josh Posted October 12, 2009 Share Posted October 12, 2009 @jenk, g is not a known modifier. in javascript it is the equivalent of preg_match_all vs. preg_match not that this is supposed to be a js solution...just sayin', that's prolly where he was coming from. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted October 12, 2009 Share Posted October 12, 2009 Ah, I see. Quote Link to comment Share on other sites More sharing options...
lihman Posted October 13, 2009 Share Posted October 13, 2009 $option_title = preg_replace('(\\[.*?\\])', '', $option_title); That should work for you Quote Link to comment Share on other sites More sharing options...
lihman Posted October 13, 2009 Share Posted October 13, 2009 I believe that the starter isn't interested in this thread anymore. I have tested my code and it works for me. Therefore, I request a mod to place a [solved] in the topic name. Thank you PS: Sorry for double posting. Just bringing it into the views of the mods Quote Link to comment Share on other sites More sharing options...
.josh Posted October 13, 2009 Share Posted October 13, 2009 so I guess all the other people before you who already gave a viable solution don't count? IT'S ALL ABOUT YOU, ISN'T IT Quote Link to comment Share on other sites More sharing options...
cags Posted October 13, 2009 Share Posted October 13, 2009 I'd have thought that as a Moderator Crayon Violent, you'd have realised that it's actually all about ME! Quote Link to comment Share on other sites More sharing options...
lihman Posted October 13, 2009 Share Posted October 13, 2009 Sorry, if u thought I meant it that way. I meant to say that I have tested my solution and it does work. So it is solved for sure. Many of the solutions above may work, but I have not tested them so i cannot say for sure. Actually, upon testing #\[.*\]# , what Crayon Violent and MrAdam have said, I would say it is not recommended. I say this because it will also remove what comes before the brackets. ex. fri[hello]day will become "day". If that does not affect you, then feel free to use it. Upon using (\\[.*?\\]) , you will get "friday" Just throwing it out there Quote Link to comment Share on other sites More sharing options...
Adam Posted October 13, 2009 Share Posted October 13, 2009 If you go back and read it all you'll see I changed the reg exp to: #\[.*?\]# echo preg_replace('#\[.*?\]#', '', 'fri[hello]day'); S'plain how that doesn't work? Quote Link to comment Share on other sites More sharing options...
lihman Posted October 13, 2009 Share Posted October 13, 2009 Or even.. #\[.*\]# Why don't you try running that through this string and see what happens: [blahblah] some random stuff [more blah] Sorry, i didn't see that post. I only looked at what Crayon Violent had written. So I stand corrected. #\[.*?\]# will work Thanks for clearing it up MrAdam Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted October 13, 2009 Share Posted October 13, 2009 Actually, upon testing #\[.*\]# , what Crayon Violent and MrAdam have said, I would say it is not recommended. I say this because it will also remove what comes before the brackets. ex. fri[hello]day will become "day". Really? $str = 'fri[hello]day'; echo $str = preg_replace('#\[.*\]#', null, $str); Aside from MrAdam correcting that post with the use of a lazy quantifier, the issue with using .* (or even .+) is demonstrated in the following: (post #14 shows the potential pitfall of this 'typically reckless' behavior - sometimes this behavior is actually beneifical.. but as a rule? ). Quote Link to comment Share on other sites More sharing options...
.josh Posted October 14, 2009 Share Posted October 14, 2009 also, I didn't suggest anything...I actually pointed out how someone's regex was flawed. You really should take the time to read stuff...I'm sure you are quite lovable, but stuff like this makes me think you should be gob smacked. 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.