Simple / Semi-Advanced preg_match???

Discussion in 'PHP' started by adbox, Feb 3, 2010.

  1. #1
    Hey guys,

    What I need to do is find all occurrences of a string of text inbetween [ and ] that does not contain another [ inside it.

    I believe preg_match is what I should want to use but I never figured out how to manipulate these.

    thanks!
    adbox
     
    adbox, Feb 3, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Maybe, something such as:

    <?php
    
    $text = <<<TEXT
    hey how are [you], im [g0ing] to test.
    TEXT;
    
    preg_match_all("|\[([a-zA-Z0-9\., ]*)\]|", $text, $occurences);
    
    //all occurences
    echo "<pre>";
    print_r($occurences);
    echo "</pre>";
    
    //number of occurences
    echo count($occurences);
    
    ?>
    PHP:
     
    Last edited: Feb 3, 2010
    danx10, Feb 3, 2010 IP
  3. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #3
    Hi,

    Try this:
    
    $s='bla[blah]sasasa[doh]qwerty';
    $arr=array();
    preg_match_all('#\[([^\[\]]+)\]#',$s,&$arr);
    if (count($arr[1])) echo implode(',',$arr[1]);
    
    PHP:
    Regards,
    Nick
     
    koko5, Feb 3, 2010 IP
  4. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #4
    actually none of them worked as needed

    lets say we went with this:
    
    
    $text = "TEXT
    hey how are [you [][][a], im [g0ing] to test.] [blabla]";
    
    //then thre should only return 3 results because there 
    //are only 3 instances where content is enclosed 
    //between [ & ] without another [ found within the sandwiched content.
    
    
    PHP:
     
    adbox, Feb 3, 2010 IP
  5. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #5
    My example returns
    with your input string.
    I didn't test other solutions.
    Regards,
    Nick
    Edit: Is this the correct result you need?
     
    koko5, Feb 3, 2010 IP
  6. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #6
    I think I am getting it to work now, but how come it sets arrays into the array, rather than the elements between the brackets themselves.
     
    adbox, Feb 3, 2010 IP
  7. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #7
    preg_match_all sets it, depend on current regular expression we use-if you can optimize/simplify regexp without dropping functionality, output array will be simpler. However, this function collects matches in array not a string :)
    Regards,
    Nick
    p.p.: try print_r($arr) for my code to see everything we get.
     
    koko5, Feb 3, 2010 IP
  8. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #8
    thanks Nick, everything is in order and working as it should!
     
    adbox, Feb 3, 2010 IP