Page 1 of 1

[SOLVED] Searching for text enclosed within square brackets

Posted: Mon Jun 21, 2004 2:32 pm
by Almighty
I have a script which needs to search a variable (which is from a mysql database e.g $row['text']) for any text which is enclosed by square brackets.
I have had a go at creating this however it doesnt put the text in the correct array, also when i try to echo out $matches[0] or any other array element it comes back with nothing.

Code: Select all

<?php
$text1 = $row['public'];
$regex = '/\[.*\]/i';
preg_match_all($regex, $text1, $matches);

?>
Any ideas?

Posted: Mon Jun 21, 2004 2:35 pm
by Illusionist
try echoing $text1, and make sure that what you want in there actually is.

Posted: Mon Jun 21, 2004 2:47 pm
by Almighty
yep it echos out the correct thing.

Posted: Mon Jun 21, 2004 2:57 pm
by feyd

Code: Select all

preg_match_all('#\[(.*?)]#s',$text1,$matches);

print_r($matches[1]);

Posted: Mon Jun 21, 2004 3:03 pm
by Almighty
Ah nice, they have seperated correctly. How would i get the different elements of the array?

Posted: Mon Jun 21, 2004 3:06 pm
by Almighty
nevermind got it, it would be $matches[0][0]

Thanks a lot to all