SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: Removing content between ( )
-
Sep 25, 2009, 11:18 #1
- Join Date
- Nov 2005
- Location
- Karachi - Pakistan
- Posts
- 1,134
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Removing content between ( )
Hello
I have a situation where i want to remove all the contents between and including ( and ).
For example
AAA(asdasdasdasdasd)BBB -> AAABBB
A90(59.093)ftg->a90ftg
please guide
-
Sep 25, 2009, 12:02 #2
Are you looking for guidance, or a code snippet to copy and paste? The problem is pretty simple and can be achieved in a variety of ways. You could use basic string functions, manual parsing of the string, a regular expression, etc. to do this. Is that guidance enough?
-
Sep 25, 2009, 12:42 #3
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Oooh, I see one of 'those' threads happening... People coming up with the most complicated ways of doing stuff!
Well the obvious regex one is
PHP Code:$String = 'AAA(asdasdasdasdasd)BBB<br />A90(59.093)ftg';
$String = preg_replace('~\([^)]*\)~', '', $String);
echo $String;
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Sep 25, 2009, 12:47 #4
- Join Date
- Aug 2009
- Posts
- 669
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I wouldn't even use a regular expression.. I'd just str_replace() !
Code:$New_String = str_replace('(asdasdasdasdasd)', '', 'AAA(asdasdasdasdasd)BBB');
although admittedly thats assuming the string is hard coded!
Saves coding that RE...
-
Sep 25, 2009, 13:46 #5
I'll see your regex and raise it via the medium of recursion with a hint of silliness.
PHP Code:<?php
$subject = "AAA(asdasdasdasdasd)BBB\nA90(59.093)ftg";
$pattern = '/(?(DEFINE)(?<_>[^()])(?<__>(?&_)*+))
\((?<___>(?&__) | \((?(R&___))\))*\)/x';
echo preg_replace($pattern, '', $subject);
?>
AAABBB
A90ftg
-
Sep 25, 2009, 14:05 #6
- Join Date
- Aug 2009
- Posts
- 669
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ok, heres my second bash.. This one does it dynamically. I'll be brave and admit it.. I've never used regular expressions, don't really understand them and tend to do everything the hard way.
So.. I figured I'd do this for everyone elses benefit if they're in the same boat:
Code:$Var = "AAA(asdasdasdasdasd)BBB->a90ftg"; print substr_replace($Var, '', strpos($Var, '('), strpos($Var, ')') -strpos($Var, '(') +1);
-
Sep 25, 2009, 20:17 #7
- Join Date
- Nov 2005
- Location
- Karachi - Pakistan
- Posts
- 1,134
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks a lot arkinstall
You understand the issue and has presented the BEST comment and solution.
-
Sep 26, 2009, 04:25 #8
Bear in mind that arkinstall's solution will fail in certain conditions, for example if there are nested parentheses. If this is likely a problem for you, the regular expression could be modified to check more precisely for proper pairs of parentheses. Would this be something you want, or is arkinstall's solution "good enough"?
Bookmarks