Forum Moderators: coopster

Message Too Old, No Replies

brackets <> screwing up my string search

         

codebreaker

2:45 am on Nov 13, 2005 (gmt 0)

10+ Year Member



I am trying to perform a match on strings of data. Each string will be a line from an email template. I want to extract the info from each header in the template. What I have works well, so long as there are no brackets <> in there, like what normally surrounds an email address.

The template looks like this:

--Begin Template
To: Joe McCarthy <joe@home.net>
From: Me <me@myhouse.net>
Subject: Hey!

This is the body of the message.
--End Template

And here it is some code for illustration.


# These are the fields I want to find (there are more)
$EmailFields[0] = 'To';
$EmailFields[1] = 'From';
$EmailFields[2] = 'Subject';

# Load in the template
$file = file("MessageTemplate.txt");

# Take each line, and regexp each header field
foreach ($file as $line_num => $line) {
foreach ($EmailFields as $Field) {

if (preg_match("/^$Field {0,1}: {0,1}(.+)$/", $line, $matches)) {

# Got a match, record the
$Headers["$Field"] = $matches[1];
echo $Headers["$Field"] . "<BR>";
}
}
}

As I said, It works just fine as long as there are not brackets. Assuming the first line of the template is

"To: Joe McCarthy joe@home.net"

then the first entry in $Header would be

"Joe McCarthy joe@home.net"

But if I enclose an email address in brackets

"To: Joe McCarthy <joe@home.net>"

then the email address (and brackets) is not added to $Headers

"Joe McCarthy " (WITH the space at the end)

Anybody have an idea why? Thanks.

a1call

3:08 am on Nov 13, 2005 (gmt 0)

10+ Year Member



Hi codebreaker,
The browser does not show items in the <>. Look at the source code and everything should be there. This might help:

<?php
$a="To: Joe McCarthy <joe@home.net>";
echo($a."<br>");
$a= explode('<', $a);
$a=implode("", $a);
$a= explode('>', $a);
$a=implode("", $a);
echo($a);
?>

Browser output:

To: Joe McCarthy
To: Joe McCarthy joe@home.net

I'm sure this can be done more efficiently and with less code than mine.

codebreaker

3:34 am on Nov 13, 2005 (gmt 0)

10+ Year Member



Hi a1call,

You know, sometimes you (actually, me) can get so wrapped up in the more difficult stuff, that you forget to check the obvious stuff. How could I have missed that?

I'm almost mad that the answer was so easy. :)

Thanks -codebreaker

a1call

4:37 am on Nov 13, 2005 (gmt 0)

10+ Year Member



Hi codebreaker,

Please don't get mad. This is about the extents of the difficulty I could help with. If you had any problem with the rest of your code, you would have to wait for a real expert to reply:)

jatar_k

6:45 am on Nov 13, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hey codebreaker

don't let it get you mad, us old fellas call it "needing an extra pair of eyes"

just means we know we missed something obvious ;)