Hey All,
Would like to get:
from:This is a pretty cool line of text!
So, basically rip out the ( and the ) and anything between then.This is a (sweet) pretty cool line (string) of text!
Thanks!
| SitePoint Sponsor |
Hey All,
Would like to get:
from:This is a pretty cool line of text!
So, basically rip out the ( and the ) and anything between then.This is a (sweet) pretty cool line (string) of text!
Thanks!





Something like this should work:
That is a basic method. If you notice I put an if in there which says that if we don't have a ), we're gonna stop looping. You may want to tweak that to act depending how you want to handle that.Code:while(($loc = strpos('(', $text)) !== false) { if(($end_loc = strpos(')', $text)) === false) break; $text = substr($text, 0, $loc) . substr($text, $endloc); }
There is also a regex method of doing this, but I'm not a regex pro so I'll let logic_earth or someone else fill you in on that method. =3
Hope this helps.
Xazure.Net - My Blog - About Programming and Web Development
Follow Me on Twitter!
Christian Snodgrass
Will strip even if the opening ( and closing ) aren't on the same line. It will strip unlimited text in between.PHP Code:$new = preg_replace('#\([^)]*\)#', '', $text);
Sweet, thanks crmalibu!
You'll have to watch out for nested pairs of parentheses. If you can wrap your head around it, a recursive pattern might come to the rescue.
Code Regex:\( # opening parenthesis (?: # followed by [^()]++ # one or more non-parenthesis characters (double plus) | # or (?R) # a nested parenthesis pair )* # zero or more times (zero for empty parenthesis pairs) \) # closing parenthesis
In PHP that would be something like:
PHP Code:$text = 'This is (a) test ()of (nested (roundy) brackets) smileys :)';
echo preg_replace('/\((?:[^()]++|(?R))*\)/', '', $text);
try javascript
Bookmarks