Extract part of a string

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
alste450
Forum Newbie
Posts: 2
Joined: Sun Oct 28, 2007 3:46 pm

Extract part of a string

Post by alste450 »

Hi,

I am looking for a code that can grab everything in between the brackets corresponding to a certain variable ($name).

For example, if my string is

$string =
"John{
john is a brown worm
}

Laura{
nancy is a blue pig
}";

and $name = "John", what I want returned is "john is a brown worm";
$name can equal Laura too, or any other name, in a list of n names, so John doesn't have to be the first name.
And the contents within the brackets can span multiple lines.

I am trying to do it with preg_match but it's not working.

Thanks for your help!
Last edited by alste450 on Sun Oct 28, 2007 4:18 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Regular Expressions.

preg_match();
alste450
Forum Newbie
Posts: 2
Joined: Sun Oct 28, 2007 3:46 pm

Post by alste450 »

Hi,

Yes indeed, I've been trying to use preg_match for last few hours, but no results. Can't find the regex expression. Could you give details?

Thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What have you tried? There are many examples which are similar in the Regex forum we have..
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

You'll probably want to use preg_match_all() in this case, as your example shows you want to capture multiple instances of the pattern, no?
Post Reply