Parse a string with an email address

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martoni

    Parse a string with an email address

    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 ParseTicketEmai l($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 (world@world.co m) bla";
    print "$TicketEma il";
    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
  • Guillaume Brocker

    #2
    Re: Parse a string with an email address

    Martoni wrote:[color=blue]
    > 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 ParseTicketEmai l($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 (world@world.co m) bla";
    > print "$TicketEma il";
    > 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?[/color]

    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

    Comment

    • Guillaume Brocker

      #3
      Re: Parse a string with an email address

      Martoni wrote:[color=blue]
      > b: does anyone have such a function anywhere for me to check out for how to do it?[/color]

      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

      Comment

      • Martoni

        #4
        Re: Parse a string with an email address

        Guillaume Brocker <guillaume.broc ker@ircad.u-strasbg.fr> writes:
        [color=blue]
        > Martoni wrote:[color=green]
        >> b: does anyone have such a function anywhere for me to check out for how to do it?[/color]
        >
        > 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 ?[/color]

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

        I'll give it a go ...

        [color=blue]
        > 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>[/color]

        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

        Comment

        • John Dunlop

          #5
          Re: Parse a string with an email address

          Guillaume Brocker wrote:
          [color=blue]
          > Martoni wrote:
          >[color=green]
          > > 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.[/color]
          >
          > preg_match( '/(\(.*\?.*\))/', $ticket, $matches );[/color]

          Guillaume Brocker wrote in
          <news:4067c9f9$ 0$5623$636a15ce @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@(Jo ck's domain)johndunl op.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
          trichotillomani a.

          --
          Jock

          Comment

          • Martoni

            #6
            Re: Parse a string with an email address

            john+usenet@joh ndunlop.info (John Dunlop) writes:
            [color=blue]
            > Guillaume Brocker wrote:
            >[color=green]
            >> Martoni wrote:
            >>[color=darkred]
            >> > 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.[/color]
            >>
            >> preg_match( '/(\(.*\?.*\))/', $ticket, $matches );[/color]
            >
            > Guillaume Brocker wrote in
            > <news:4067c9f9$ 0$5623$636a15ce @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@(Jo ck's domain)johndunl op.info
            >
            > Of course, if parentheses aren't allowed before or after the
            > parenthesised email address, your pattern above is almost there.[/color]

            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

            Comment

            • John Dunlop

              #7
              Re: Parse a string with an email address

              Martoni wrote:
              [color=blue]
              > john+usenet@joh ndunlop.info (John Dunlop) writes:
              >[color=green]
              > > Guillaume Brocker wrote in
              > > <news:4067c9f9$ 0$5623$636a15ce @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.[/color]
              >
              > 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?[/color]

              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 (foo@bar.exampl e) baz (bum) bee.',
              $matches)

              The content of $matches[0] is:

              (foo@bar.exampl e) baz (bum)
              [color=blue]
              > One problem I see is that the paranthesis are kept, not dropped.[/color]

              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

              Comment

              • Martoni

                #8
                Re: Parse a string with an email address

                John Dunlop <john+usenet@jo hndunlop.info> writes:
                preg_match([color=blue]
                > '`\(.*@.*\)`',
                > 'Mr. F. Bar (foo@bar.exampl e) baz (bum) bee.',
                > $matches)
                >
                > The content of $matches[0] is:
                >
                > (foo@bar.exampl e) baz (bum)[/color]

                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

                Comment

                • John Dunlop

                  #9
                  Re: Parse a string with an email address

                  Martoni wrote:
                  [color=blue]
                  > 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.[/color]

                  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

                  Comment

                  • Martoni

                    #10
                    Re: Parse a string with an email address

                    John Dunlop <john+usenet@jo hndunlop.info> writes:
                    [color=blue]
                    > Martoni wrote:
                    >[color=green]
                    >> 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.[/color]
                    >
                    > 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?[/color]

                    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

                    Comment

                    Working...