Selecting specific things from a variable.

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
windwaker
Forum Newbie
Posts: 21
Joined: Sun Feb 13, 2005 11:18 am

Selecting specific things from a variable.

Post by windwaker »

So, let's say I have a variable, $var, and:

Code: Select all

$var = 'useless text and їtestyeah]';
I want to select the value between [ and ] and put it in another variable. Is there any way to do this?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

yes.

you could for example use strpos to determine the position of [ and ]. And then use substr to get the part between those positions..

or you could use preg_match to match things between [ and ] and then use the $matches...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

personally, I'd only use preg_match_all for it.. if you have multiple 'tags' you want to search for.. or unknown 'tags'.
Post Reply