Re: Splitting a string ...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Op 3/15/10 1:54 AM, Ashley M. Kirchner schreef:
> I'm not a regexp person (wish I was though), and I'm hoping someone can give
> me a hand here.  Consider the following strings:
> 
>  
> 
> -          domain\username@xxxxxxxxxxx
> 
> -          domain\username
> 
> -          the same as above but with / instead of \  (hey, it happens)
> 
> -          username@xxxxxxxxxxx
> 
> -          username
> 
>  
> 
> Essentially I have a sign-up form where folks will be typing in their
> username.  The problem is, in our organization, when you tell someone to
> enter their username, it could end up being any of the above examples
> because they're used to a domain log in procedure where in some cases they
> type the whole thing, in other cases just the e-mail, or sometimes just the
> username.
> 
>  
> 
> So what I'd like is a way to capture just the 'username' part, regardless of
> what other pieces they put in.  In the past I would write a rather
> inefficient split() routine and eventually get what I need.  With split()
> getting deprecated, I figured I may as well start looking into how to do it
> properly.  There's preg_split(), str_split(), explode() . possibly others.
> 
>  
> 
> So, what's the proper way to do this?  How can I capture just the part I
> need, regardless of how they typed it in?
> 

<?php

$inputs = array(
	// double slashes just due to escaping in literal strings
	"domain\\username@xxxxxxxxxxx",
	"domain\\username",
	"domain/username@xxxxxxxxxxx",
	"domain/username",
	"username@xxxxxxxxxxx",
	"username",
);
foreach ($inputs as $input) {
	// four slashes = two slashes in the actual regexp!
	preg_match("#^(?:.*[\\\\/])?([^@]*)(?:@.*)?$#", $input, $match);
	var_dump($match[1]);
}

?>

... just off the top of my head, probably could be done better than this.
I would recommend reverse engineering the given regexp to find out what
it's doing exactly ... painstaking but worth it to go through it char by char,
it might be the start of a glorious regexp career :)

>  
> 
> Thanks!
> 
>  
> 
> A
> 
> 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux