split word by space char

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
srdva59
Forum Commoner
Posts: 77
Joined: Sun Feb 15, 2009 10:58 am

split word by space char

Post 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
:)
Last edited by Benjamin on Sun May 03, 2009 4:24 am, edited 2 times in total.
Reason: Turned off URLs, added code tags.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: split word by space char

Post 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.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: split word by space char

Post 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'
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: split word by space char

Post 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.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: split word by space char

Post by Apollo »

And otherwise, you're also safe with

Code: Select all

$s = preg_replace('/ .*$/','',trim($s));
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: split word by space char

Post 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:
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: split word by space char

Post 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.
Last edited by McInfo on Tue Jun 15, 2010 3:13 pm, edited 1 time in total.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: split word by space char

Post 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.
srdva59
Forum Commoner
Posts: 77
Joined: Sun Feb 15, 2009 10:58 am

Re: split word by space char

Post by srdva59 »

thanks a lot all that help me a lot :)
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: split word by space char

Post 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.
Post Reply