[SOLVED] Searching for text enclosed within square brackets

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Almighty
Forum Newbie
Posts: 4
Joined: Mon Jun 21, 2004 2:32 pm

[SOLVED] Searching for text enclosed within square brackets

Post 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?
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

try echoing $text1, and make sure that what you want in there actually is.
Almighty
Forum Newbie
Posts: 4
Joined: Mon Jun 21, 2004 2:32 pm

Post by Almighty »

yep it echos out the correct thing.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

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

print_r($matches[1]);
Almighty
Forum Newbie
Posts: 4
Joined: Mon Jun 21, 2004 2:32 pm

Post by Almighty »

Ah nice, they have seperated correctly. How would i get the different elements of the array?
Almighty
Forum Newbie
Posts: 4
Joined: Mon Jun 21, 2004 2:32 pm

Post by Almighty »

nevermind got it, it would be $matches[0][0]

Thanks a lot to all
Post Reply