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
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:
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
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:
My example returns with your input string. I didn't test other solutions. Regards, Nick Edit: Is this the correct result you need?
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.
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.