SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Strip everything between the ( and ) characters

  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2003
    Location
    California
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Strip everything between the ( and ) characters

    Hey All,

    Would like to get:

    This is a pretty cool line of text!
    from:

    This is a (sweet) pretty cool line (string) of text!
    So, basically rip out the ( and the ) and anything between then.

    Thanks!

  2. #2
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    Something like this should work:
    Code:
    while(($loc = strpos('(', $text)) !== false) {
        if(($end_loc = strpos(')', $text)) === false)
          break;
    
        $text = substr($text, 0, $loc) . substr($text, $endloc);
    }
    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.

    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.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $new preg_replace('#\([^)]*\)#'''$text); 
    Will strip even if the opening ( and closing ) aren't on the same line. It will strip unlimited text in between.

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2003
    Location
    California
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sweet, thanks crmalibu!

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,356
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    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); 
    Salathe
    Software Developer and PHP Documentation Team.

  6. #6
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try javascript

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
  •