Simple replacement question

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
cjswanson1355
Forum Newbie
Posts: 10
Joined: Wed Nov 05, 2008 3:13 pm

Simple replacement question

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Simple replacement question

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

Re: Simple replacement question

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Simple replacement question

Post by requinix »

Same thing, different function :)
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Simple replacement question

Post by greyhoundcode »

Yeah, wouldn't have bothered except it didn't look like anyone had answered when I started my reply :roll:
Post Reply