Page 1 of 1

Extract part of a string

Posted: Sun Oct 28, 2007 3:48 pm
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!

Posted: Sun Oct 28, 2007 4:05 pm
by feyd
Regular Expressions.

preg_match();

Posted: Sun Oct 28, 2007 4:13 pm
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!

Posted: Sun Oct 28, 2007 4:23 pm
by feyd
What have you tried? There are many examples which are similar in the Regex forum we have..

Posted: Sun Oct 28, 2007 4:43 pm
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?