SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict blizzman24's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A script to remove anything between ()?

    I have a list and some of the listing have parentheses with words in between them. I want to remove the parentheses and the words in between. Anyone know a simple script for that?

  2. #2
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not quite sure what you mean by list but here's on idea

    Passing just the individual item in the list
    function deleteps(list){
    if($list[0] == '(' )
    return '';
    return $list;
    }

  3. #3
    SitePoint Addict blizzman24's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I enter a list of words in a html form and it passes it to a php page which processes it.

    apple1
    apple2 (grass)
    apple3 (water)
    apple4

    I want it to remove (grass) and (water) parentheses too and leave...

    apple1
    apple2
    apple3
    apple4

  4. #4
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, so how is it stored:
    $val1 = $_POST['apple1'];
    ...
    $val4 = $_POST['apple4'];
    if this is the case
    $val4 = deleteps($_POST['apple4']);

    function deleteps(list){
    for($i = 0; $i < count($list); $i++)
    if($list[$i] == '(' )
    return substr($list, 0, $i-1);
    return $list;
    }

    or if its stored in array

    for($i = 0; $i < count($array); $i++){
    $array[$i] = deleteps($array[$i]);
    }

  5. #5
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $list_r explode"\n"$list );
    foreach( 
    $list_r as $item ) {
        
    $item str_replace"(*)"""$item );
    }
    $list implode"\n"$list_r ); 
    I think the wildcard will work, if not let me know I have another trick using substr_replace() thats just a little longer.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Code PHP:
    <?php
     
    $list = array('apple1', 'apple2 (grass)', 'apple3 (water)', 'apple4');
    $list = preg_replace('/\\s+\\([^\\(]+\\)/i', '', $list);

    Also removes the sapce before "("
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Actually you don't even need to put the list into an array.
    Code php:
    <?php
     
    $list = 'apple1
    apple2 (grass)
    apple3 (water)
    apple4';
     
    $list = "apple1\napple2 (grass)\napple3 (water)\napple4";
     
    $list = preg_replace('/\\s+\\([^\\(]+\\)/i', '', $list);
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



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
  •