Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with preg_match

Status
Not open for further replies.

linuxMaestro

Instructor
Jan 12, 2004
183
US
I am trying to parse the email address from the "From" head in an email using preg_match:
From: "Tom" <tom@yahoo.com>

I have this syntax but it is not matching it:
if (preg_match("/^From: .* (<.*>))/", $lines[$i], $matches)) {

What should the patten be?
("/^From: .* (<.*>))/",
 
Thanks for the link, it went through it but am still having difficulties:
This matches everything after the from:
/^From:(.*)/"
Ex. "Name Here" <email@domain.com>"

But I am trying to just match the email, I tried all of the following, but none work:
/^From:(\<+.*+\>)/"
/^From:+.*+(<+.*+>)/"
/^From:+.*+(\<+.*+\>)/"
/^From:+(.*+(\<+.*+\>))/"
/^From:+(.*+\<+.*+\>)/"
/^From:+(.*+(<+.*+>))/"
/^From:+(.*+<+.*+>)/"

What am I doing wrong?
/^From: start with From:
+.* match 0 or more characters before
(\<+.*+\>) 0 or more characters enclosed in angle brackets before
/" End of line

A little help please.
 
/^From:(<[^>]+>)/"

does this work?

you have commented the < tag, that is not necessary...


Known is handfull, Unknown is worldfull
 
/^From:(<[^>]+>)/"

does this work?

you have commented the < tag, that is not necessary...


Known is handfull, Unknown is worldfull
 
these work for me (examples for ereg and preg syntax):

Code:
$srch = "From: John Smith <john.smith@domain.com>";
$ereg = "<(.+)>";  //ereg does not require perl syntax
$preg = "/<(.+)>/"; //preg requires perl syntax
ereg($ereg,$srch,$array); //returns matches in a zero based array but starts filling from index = 1
preg_match($preg,$srch,$array2); //returns matches in a zero based array but starts filling from index = 1

print_r($array); //index 1 should contain the string
echo "<br><br>";
print_r($array2); //index1 should contain the string

hth
Justin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top