Grab text between two strings

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Grab text between two strings

Post by d3ad1ysp0rk »

I have a block of text, and I need to end up with an array of values.

Text will look something like this:

Code: Select all

blah blah lbah
blah blah lbah
blah blah lbah
To: Justin@email.com
blah blah lbah

blah blah lbah
blah blah lbah
To: Michelle@email.com
blah blah lbah
blah blah lbah
From that, I need to get an array of the emails, so, match whatever is between "To: " and "\n"?

Could anyone give me some hints as how to do this effeciently?
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*to:\s*.*?$#im', $yourTest, $matches);
print_r($matches);
Post Reply