1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How do I get the values between two brackets?

Discussion in 'PHP' started by Imozeb, Mar 11, 2010.

  1. #1
    How do I get the values between two brackets? For example:

    $var = 'name(bob) | title(bar) | value(5)';

    What code would I need to use if I want to get the values 'bob', 'bar', and '5'.
    I thought I could use strpos() and strstr() to get the values but my code isn't working!

    Plz Help!!!

    ~imozeb
     
    Imozeb, Mar 11, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    <?php
    
    $var = 'name(bob) | title(bar) | value(5)';
    
    
    preg_match_all('~([^ ]*)\(.*\)~U', $var, $values);
    
    print_r($values[1]);
    
    ?>
    PHP:
     
    danx10, Mar 11, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'm new to regexps. How do you dump the variables into separete containers. For example $name = name value, $title = title value, and $value = value value? And is there anyway I can stop searching for new text after it has found those three values?
     
    Imozeb, Mar 11, 2010 IP
  4. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Is there anyway I can do it using strpos() because when I changed $var to 'name(bob) | title(bar) | value(5) # QWERTYUIOP{}|":LKJHGFDSAZXCVBNM<>?+_)(*&^%$#@!234567890-=~`' it printed out all that extra text. Is there anyway preg_match_all can work to fix this, mabye like stopping at the third value.

    Thanks, Danx10!
     
    Last edited: Mar 11, 2010
    Imozeb, Mar 11, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    <?php
    
    $var = 'name(bob) | title(bar) | value(5)';
    
    preg_match_all('~([^ ]*)\(.*\)~U', $var, $values);
    
    $name = $values[1][0];
    
    $title = $values[1][1];
    
    $value = $values[1][2];
    
    ?>
    PHP:
     
    danx10, Mar 11, 2010 IP
    Imozeb likes this.