By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,746 Members | 1,893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,746 IT Pros & Developers. It's quick & easy.

Parse a string with an email address

P: n/a
I need to parse a string with an embedded email address.
The string always has the format NAME (name@domain) SOMETEXT.
What I need to get is the email address as name@domain.

I came up with this (I know it's broken, but it's a first start):

<?php

function ParseTicketEmail($ticket)
{
global $TicketEmail;
for ($i=0; $i< strlen($ticket); $i++){
//we need to pass everything between ( and ) to $TicketEmail nothing else
do {
$ticket[$i] = $discarded;
} while ($ticket[$i] != '(');
while ($ticket[$i] != ')'){
$TicketEmail = $TicketEmail . $ticket[$i];
}
}
return $TicketEmail;
}

$ticket = "hello (wo***@world.com) bla";
print "$TicketEmail";
print "$discarded";
?>

But that doesn't return anything for either $TicketEmail or $discarded.
So
a: why doesn't it return anything for either string? (It's too early in the morning here)
b: does anyone have such a function anywhere for me to check out for how to do it?

/M.
--
Martin Skj÷ldebrand
Family site: http://www.skjoldebrand.org
"Art" site: http://martoni.deviantart.com
Public key available at: http://wwwkeys.pgp.net
Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Martoni wrote:
I need to parse a string with an embedded email address.
The string always has the format NAME (name@domain) SOMETEXT.
What I need to get is the email address as name@domain.

I came up with this (I know it's broken, but it's a first start):

<?php

function ParseTicketEmail($ticket)
{
global $TicketEmail;
for ($i=0; $i< strlen($ticket); $i++){
//we need to pass everything between ( and ) to $TicketEmail nothing else
do {
$ticket[$i] = $discarded;
} while ($ticket[$i] != '(');
while ($ticket[$i] != ')'){
$TicketEmail = $TicketEmail . $ticket[$i];
}
}
return $TicketEmail;
}

$ticket = "hello (wo***@world.com) bla";
print "$TicketEmail";
print "$discarded";
?>

But that doesn't return anything for either $TicketEmail or $discarded.
So
a: why doesn't it return anything for either string? (It's too early in the morning here)
b: does anyone have such a function anywhere for me to check out for how to do it?


Why are you trying to use a self-made string parsing routine for such
jobs when _regular expression pattern matching_ can do it for you in one
single call ?

Here is a sample that should work:
preg_match( '/(\(.*\?.*\))/', $ticket, $matches );

For later information about *preg_match* and *regular expression
expression* see followings:
<http://fr.php.net/manual/en/function.preg-match.php>
<http://fr.php.net/manual/en/ref.pcre.php>

--
Guillaume Brocker
Jul 17 '05 #2

P: n/a
Martoni wrote:
b: does anyone have such a function anywhere for me to check out for how to do it?


Why are you trying to use a self-made string parsing routine for such
jobs when _regular expression pattern matching_ can do it for you in one
single call ?

Here is a sample that should work:
preg_match( '/(\(.*@.*\))/', $ticket, $matches );

For later information about *preg_match* and *regular expressions*, see
followings:
<http://fr.php.net/manual/en/function.preg-match.php>
<http://fr.php.net/manual/en/ref.pcre.php>

--
Guillaume Brocker
Jul 17 '05 #3

P: n/a
Guillaume Brocker <gu***************@ircad.u-strasbg.fr> writes:
Martoni wrote:
b: does anyone have such a function anywhere for me to check out for how to do it?
Why are you trying to use a self-made string parsing routine for such
jobs when _regular expression pattern matching_ can do it for you in
one single call ?


Of course that is the way.
Why I didn't use reg exp? I get that allergical perl itch...
(I hate reg exps).
Here is a sample that should work:
preg_match( '/(\(.*@.*\))/', $ticket, $matches );
I'll give it a go ...

For later information about *preg_match* and *regular expressions*,
see followings:
<http://fr.php.net/manual/en/function.preg-match.php>
<http://fr.php.net/manual/en/ref.pcre.php>


And I'll have a look at it ...

/M.

--
Martin Skj÷ldebrand
Family site: http://www.skjoldebrand.org
"Art" site: http://martoni.deviantart.com
Public key available at: http://wwwkeys.pgp.net
Jul 17 '05 #4

P: n/a
Guillaume Brocker wrote:
Martoni wrote:
I need to parse a string with an embedded email address.
The string always has the format NAME (name@domain) SOMETEXT.
What I need to get is the email address as name@domain.


preg_match( '/(\(.*\?.*\))/', $ticket, $matches );


Guillaume Brocker wrote in
<news:40**********************@news.free.fr>:

| preg_match( '/(\(.*@.*\))/', $ticket, $matches );

There's no need to use a caturing subpattern; $matches[0] contains the
full pattern match.

A right parenthesis after the parenthesised email address would muck
up the matched address. Greedy matching will gobble everything till
the last right parenthesis in the string. Simply inverting the
greediness doesn't fix anything. Consider my address:

john+usenet@(Jock's domain)johndunlop.info

Of course, if parentheses aren't allowed before or after the
parenthesised email address, your pattern above is almost there.

In trying to match email addresses with regular expressions alone,
there are two certainties: (1) failure, and (2) an onslaught of
trichotillomania.

--
Jock
Jul 17 '05 #5

P: n/a
jo*********@johndunlop.info (John Dunlop) writes:
Guillaume Brocker wrote:
Martoni wrote:
> I need to parse a string with an embedded email address.
> The string always has the format NAME (name@domain) SOMETEXT.
> What I need to get is the email address as name@domain.


preg_match( '/(\(.*\?.*\))/', $ticket, $matches );


Guillaume Brocker wrote in
<news:40**********************@news.free.fr>:

| preg_match( '/(\(.*@.*\))/', $ticket, $matches );

There's no need to use a caturing subpattern; $matches[0] contains the
full pattern match.

A right parenthesis after the parenthesised email address would muck
up the matched address. Greedy matching will gobble everything till
the last right parenthesis in the string. Simply inverting the
greediness doesn't fix anything. Consider my address:

john+usenet@(Jock's domain)johndunlop.info

Of course, if parentheses aren't allowed before or after the
parenthesised email address, your pattern above is almost there.


Not having experience with this, do you mean that something like:
Name (email) Some text (more text) even more text

would make the preg_match to fail? One problem I see is that the paranthesis
are kept, not dropped.
/M.
--
Martin Skj÷ldebrand
Family site: http://www.skjoldebrand.org
"Art" site: http://martoni.deviantart.com
Public key available at: http://wwwkeys.pgp.net
Jul 17 '05 #6

P: n/a
Martoni wrote:
jo*********@johndunlop.info (John Dunlop) writes:
Guillaume Brocker wrote in
<news:40**********************@news.free.fr>:

| preg_match( '/(\(.*@.*\))/', $ticket, $matches );

Of course, if parentheses aren't allowed before or after the
parenthesised email address, your pattern above is almost there.
Not having experience with this, do you mean that something like:
Name (email) Some text (more text) even more text

would make the preg_match to fail?


Yes. The pattern is greedy, so matches from the first left
parenthesis to the last right parenthesis in the subject string.
Consider:

preg_match(
'`\(.*@.*\)`',
'Mr. F. Bar (fo*@bar.example) baz (bum) bee.',
$matches)

The content of $matches[0] is:

(fo*@bar.example) baz (bum)
One problem I see is that the paranthesis are kept, not dropped.


Aye, that's the problem. If parentheses are only used to demarcate
the address, the problem doesn't exist. Simply capture everything
from the first left parenthesis to the last right parenthesis:

preg_match(
'`\(.*\)`s',
$subject,
$matches)

--
Jock
Jul 17 '05 #7

P: n/a
John Dunlop <jo*********@johndunlop.info> writes:
preg_match(
'`\(.*@.*\)`',
'Mr. F. Bar (fo*@bar.example) baz (bum) bee.',
$matches)

The content of $matches[0] is:

(fo*@bar.example) baz (bum)


Hmm,
Not good at all.
I've never seen paranthesis used in our trouble tickets but if someone decides to enter his/her new novella as a trouble ticket then end it all with (just like Charlie experienced last week) then it will be a very nasty bug indeed.

Ofcourse one could boldly truncate the ticket string at an educated guess to include the "header". Or coming to think of it couldn't one read the string to get the character sequence number of the first ")" then truncate the string at "that number"+1 then do the preg_match.

/M.

--
Martin Skj÷ldebrand
Family site: http://www.skjoldebrand.org
"Art" site: http://martoni.deviantart.com
Public key available at: http://wwwkeys.pgp.net
Jul 17 '05 #8

P: n/a
Martoni wrote:
Or coming to think of it couldn't one read the string to get the
character sequence number of the first ")" then truncate the string
at "that number"+1 then do the preg_match.


Non-greedy matching would suffice if it were that simple. But it's
not. A right parenthesis can appear as part of an address.

Why does a single string contain the name, address and other data?

--
Jock
Jul 17 '05 #9

P: n/a
John Dunlop <jo*********@johndunlop.info> writes:
Martoni wrote:
Or coming to think of it couldn't one read the string to get the
character sequence number of the first ")" then truncate the string
at "that number"+1 then do the preg_match.


Non-greedy matching would suffice if it were that simple. But it's
not. A right parenthesis can appear as part of an address.

Why does a single string contain the name, address and other data?


For historical reasons the ticket is formated that way.
I will "soon" start rewriting the whole thing, but for maintenance and adding
some stuff, mostly minor features, I have work around the current faulty design.
The ticket includes name and email of the person submitting it, and I thought
it would be neat in some cases to extract that info.

/M.

--
Martin Skj÷ldebrand
Family site: http://www.skjoldebrand.org
"Art" site: http://martoni.deviantart.com
Public key available at: http://wwwkeys.pgp.net
Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.