Page 1 of 1

split word by space char

Posted: Sat May 02, 2009 7:46 am
by srdva59
hi,
i have a list of words like this:

Code: Select all

 
theisml35@berkleyintl.com Mon Apr 27 12
teleut@qdusa.com Tue Apr 28 15:43:24 20
reoccupyings2@encyclapidea.com Tue Apr 
tools06@hammourilaw.com Tue Apr 28 16:1
airshipt3@hcrx.com Tue Apr 28 09:38:31 
testing.tech123@gmail.com Wed Apr 29 10
rearrangedk6814@tracc5.com Wed Apr 29 2
papillai@brockandbecca.com Mon Apr 27 0
ihogqfk@bostechcorp.com Wed Apr 29 21:1
grimeix334@johnnyschuetten.com Fri May 
handkerchiefsvy68@brandtpmc.com Mon Apr
reorganizing866@fonderephar.com Wed Apr
 
and i want remove the wed apr and all data after the e-mail and
keep only the e-mails.
how can i do that?
thanks a lot for your help
:)

Re: split word by space char

Posted: Sat May 02, 2009 8:24 am
by greyhoundcode
How about:

Code: Select all

$string  = 'theisml35@berkleyintl.com Mon Apr 27 12';
$pattern = '/([a-zA-Z0-9\._]+@[a-zA-Z0-9\.\-_]+)/';
$result  = array();
 
if (preg_match($pattern, $string, $result))
{
    echo $result[0];
}
Tightening up $pattern to suit your needs.

Re: split word by space char

Posted: Sat May 02, 2009 9:31 am
by Apollo
Perhaps simpler:

Code: Select all

$s = 'theisml35@berkleyintl.com Mon Apr 27 12';
 
$s = preg_replace('/ .*$/','',$s);
 
// $s is now 'theisml35@berkleyintl.com'

Re: split word by space char

Posted: Sat May 02, 2009 10:39 am
by greyhoundcode
Apollo wrote:

Code: Select all

$s = 'theisml35@berkleyintl.com Mon Apr 27 12';
 
$s = preg_replace('/ .*$/','',$s);
 
// $s is now 'theisml35@berkleyintl.com'
Yeah I would go with Apollo's if you can guarantee the format of each line, ie there's no chance of any whitespace creeping in before the email address and so on.

Re: split word by space char

Posted: Sat May 02, 2009 11:31 am
by Apollo
And otherwise, you're also safe with

Code: Select all

$s = preg_replace('/ .*$/','',trim($s));

Re: split word by space char

Posted: Sat May 02, 2009 12:10 pm
by greyhoundcode
It would indeed, I was thinking also of the data being shuffled into different orders or even being XML-ised (is that a real term?!) or whatever, and simply extracting the email address:

Code: Select all

Mon Apr 27 12 theisml35@berkleyintl.com

Code: Select all

<date>Mon Apr 27 12</date><email>theisml35@berkleyintl.com</email>
In which case my solution is general purpose enough to cope - I concede that isn't what the OP was asking for though :wink:

Re: split word by space char

Posted: Sat May 02, 2009 10:54 pm
by McInfo
There is an obscure part of RFC 2822 "Internet Message Format" that allows spaces in email addresses. See Appendix A.5. Not that anyone takes advantage of it...

Edit: This post was recovered from search engine cache.

Re: split word by space char

Posted: Sun May 03, 2009 4:19 am
by greyhoundcode
True enough, but then if we want to validate properly it's going to be a very long regex indeed!

As one article I read highlighted, an email address such as pied-piper@hamlin.doggy is perfectly valid under RFCs 822, 2822 and 3696, however we know that there is no such TLD as .doggy, so full-on validation isn't always the bee's knees.

Re: split word by space char

Posted: Sun May 03, 2009 5:45 am
by srdva59
thanks a lot all that help me a lot :)

Re: split word by space char

Posted: Sun May 03, 2009 9:37 am
by McInfo
greyhoundcode wrote:True enough, but then if we want to validate properly it's going to be a very long regex indeed!
Thanks for posting that link.

Edit: This post was recovered from search engine cache.