Page 1 of 1

Simple replacement question

Posted: Wed Feb 18, 2009 1:37 pm
by cjswanson1355
I am trying to modify a login screen to prevent people from adding their e-mail to the end of their login name. Basically, I want

cjswanson1355@hotmail.com

to be filtered and return

cjswanson1355

But I am having difficulty in finding exactly what function I am supposed to be using.

Re: Simple replacement question

Posted: Wed Feb 18, 2009 1:45 pm
by requinix
You can filter out a @ (and everything after) with substr and strcspn:

Code: Select all

$name = "cjswanson1355@hotmail.com";
echo substr($name, 0, strcspn($name, "@"));

Re: Simple replacement question

Posted: Wed Feb 18, 2009 1:48 pm
by greyhoundcode
How about something like:

Code: Select all

function Filter_Email($email)
{
    return substr($email, 0, strpos($email, '@'));
}
 
echo Filter_Email('jobby@toilet.net');
 
// Output:
// jobby

Re: Simple replacement question

Posted: Wed Feb 18, 2009 2:03 pm
by requinix
Same thing, different function :)

Re: Simple replacement question

Posted: Thu Feb 19, 2009 5:23 am
by greyhoundcode
Yeah, wouldn't have bothered except it didn't look like anyone had answered when I started my reply :roll: