tags:

views:

79

answers:

5

Ok this one should be incredibly easy, but I don't know what I'm looking for...

I want to split a string up between two characters

$string = "blah blah blah (blorp) blah blah (bloop) blah blah (bam)";

retruns :

array
1 -> blorp
2 -> bloop
3 -> bam

I dont need any of the blah blahs just everything within parenthesis.

Thanks!

Arthur

+2  A: 

You could use preg_match_all with something like (just a quick draft...):

preg_match_all("|\([^)]+\)|", $string, $result_array);
jeroen
+1  A: 

If you want everything in parenthesis, then you should use regular expressions. In your case, this will do the thick:

preg_match_all('/\(.*?\)/', $string, $matches);
Tatu Ulmanen
+1  A: 

http://php.net/manual/en/function.preg-match-all.php

$matches = array();
$num_matched = preg_match_all('/\((.*)\)/U', $input, $matches);
jestro
+4  A: 

You can do this with a regular expression:

$string = "blah blah blah (blorp) blah blah (bloop) blah blah (bam)";
preg_match_all("/\((.*?)\)/", $string, $result_array);

print_r( $result_array[1] ); // $result_array[0] contains the matches with the parens

This would output:

Array
(
    [0] => blorp
    [1] => bloop
    [2] => bam
)

My regular expression uses a non-greedy selector: (.*?) which means it will grab as "little" as possible. This keeps it from eating up all the ) and grabbing everything between the opening ( and the closing ) how ever many words away.

Doug Neiner
+1  A: 

all using regex, here's one without regex

$string = "blah blah blah (blorp) blah blah (bloop) blah blah (bam)";
$s = explode(")",$string);
foreach ( $s as $k=>$v ){
    $m= strpos($v,"(" );
    if ($m){
        print substr( $v, $m+1  )  . "\n"  ;
    }
}