SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    if ($zee == "Guru") { $zee--;}
    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

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    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?
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    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
    But I'd love to see some inventive over-complicated ways of going about this :P
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Guru
    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');
    There... done in one line although admittedly thats assuming the string is hard coded!

    Saves coding that RE...

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    I'd love to see some inventive over-complicated ways of going about this :P
    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);
    ?>
    The output being:
    AAABBB
    A90ftg
    Salathe
    Software Developer and PHP Manual Author.

  6. #6
    SitePoint Guru
    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);

  7. #7
    if ($zee == "Guru") { $zee--;}
    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.

  8. #8
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by zeeshanhashmi View Post
    Thanks a lot arkinstall

    You understand the issue and has presented the BEST comment and solution.
    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"?
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •