SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2003
    Location
    California
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation strip everything from a string except email addresses

    Hey All,

    I have the desire to throw a block of text into a textfield (the header of an email) and have it return only the email addresses listed.

    Could anybody whip together the code to do this?

    Thanks!

  2. #2
    SitePoint Zealot shoorace's Avatar
    Join Date
    Jun 2005
    Location
    Florida
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does this JS validation work for you?

    Code JavaScript:
    function test(txtEmail) {
      var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
      var str  = txtEmail.value;
      if(str.match(regex)){
         return true;
      }else{
        txtEmail.value = "";
        return false;
      }
    }

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2003
    Location
    California
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, there are usually more then one email address within the header of an email.... so, I do not see how that would help. But thanks.

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    JS is never a secure solution. Ever.

    Here's the solution:
    PHP Code:
    <?php
    $matches 
    = array();
    preg_match('/([A-Za-z0-9.\-_]*)@([A-Za-z0-9\-_]*)\.([A-Za-z\.]*)/'$yourtext$matches);
    $emails = array();
    foreach(
    $matches as $match){
        
    $emails[] = $match[0];
    }
    ?>
    And to show an example with my (recently increasingly useful) regex tester:
    http://www.thefullresource.com/tools...ther+text+blah
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2003
    Location
    California
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    Thanks for the code, sadly, it does not work.


    Tried playing around with it, but couldn't make it work
    Last edited by Yokhannan; Sep 26, 2008 at 13:27. Reason: remove url

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ah, it's preg_match_all you want:
    PHP Code:
    <?php 
    $matches 
    = array(); 
    preg_match_all('/([A-Za-z0-9.\-_]*)@([A-Za-z0-9\-_]*)\.([A-Za-z\.]*)/'$yourtext$matches); 
    $emails = array(); 
    foreach(
    $matches as $match){ 
        
    $emails[] = $match[0]; 

    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2003
    Location
    California
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, that got me somewhere!

    Here is the code I used to produce the results I was after:
    PHP Code:
    $matches = array();
    preg_match_all('/([A-Za-z0-9.\-_]*)@([A-Za-z0-9\-_]*)\.([A-Za-z\.]*)/'$email_list$matches);
    $emails = array();
    foreach(
    $matches[0] as $match)
    {
     
    $emails[] = $match;
    }
    $emails array_unique($emails); 
    Thanks a bunch arkinstall

  8. #8
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your preg_match statement has one small flaw. I work for a school district and my e-mail address is in the format of "someone@wasu.k12.il.us". Using your preg_match statement this address would match to "someone@wasu.k" If you change it from
    PHP Code:
    preg_match_all('/([A-Za-z0-9.\-_]*)@([A-Za-z0-9\-_]*)\.([A-Za-z\.]*)/'$email_list$matches); 
    to
    PHP Code:
    preg_match_all('/([A-Za-z0-9.\-_]*)@([A-Za-z0-9\-_]*)\.([A-Za-z0-9\.]*)/'$email_list$matches); 
    which is just adding a numeric match to the last part, it will match addresses of this type just fine.

    Hope that helps some.

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very bad regex! Email addresses can have alphanumeric characters as well as these: ! # $ &#37; & ' * + - / = ? ^ _ ` . { | } ~

    These are also valid:
    "Bobby\@Tables"@example.com
    "Frank\ Danger"@example.com
    "Peter\\ Glo"@example.com
    "\␤#!^\␡\ apple"@example.com
    "\<?php\ echo\ sprintf\('%0.2f',\ 4.3433\)\;\ ?\>"@example.com

    Gmail allows + in email addresses!

    RFC 3696 plus errata

  10. #10
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Also valid is

    someone@127.0.0.1
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  11. #11
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sk89q is correct. Although characters such as + * $ ^ & % = ~ ! ? { | } \ are valid in e-mail addresses, in most cases it is not recommended to use them due to their special significance in certain OSs and applications. But they are valid and may be seen on occasion.

    This regex should be more appropriate and should validate most any e-mail address including most of the oddball ones.

    PHP Code:
    '/[-_a-z0-9\'+*$^&%=~!?{|}\\\]++(?:\.[-_a-z0-9\'+*$^&%=~!?{|}\\\]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?/iD' 
    Also, after looking at the code some more, I realized that this could be shortened a bit.

    Try this:
    PHP Code:
    $matches = array();
    preg_match_all('/[-_a-z0-9\'+*$^&%=~!?{|}\\\]++(?:\.[-_a-z0-9\'+*$^&%=~!?{|}\\\]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?/iD'$email_list$matches);
    $emails = array();
    $emails array_unique($matches[0]); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •