Extract part of string


(Luke Pullin) #1

I am wanting to be able to extract from a users email address everything prior to the @. This is what i am going to use to generate a users username.

For example; a user registers with ooaat@yourdomain.com, I want to be able to create a username 'ooaat'

Obviously I would need to be able to validate that the username is unique and if not add a number onto the end of 'ooaat1'.

I know with mysql you can use left() function but that would mean I would have to save the email first. Then grab it again with AR and create. Is there another way?

reference on left()

http://dev.mysql.com…l#function_left


(Spyros) #2

You can overide the

protected function beforeSave() function in your model class use php split function to get the first part of the email and then check if the name already exists. Then create the username or if it exists change it to something unique


(Luke Pullin) #3

Correct I know in principle what needs to be done, its just I'm not much of a coder so am still learning.

This is what as I was going to put into the model;

public function autoUserName()


    {


        $this->username = preg_replace('/([^@]*).*/', '$1', $this->email);


    }

this works fine obviously until i test where a user with same prefix occurs,

for example;

test@test.com

test@testing.com

So if anyone could help me write a while statement which would check to see if username already exists if so add a number to end of username.

Cheers


(Spyros) #4

You can use something like this

Assume that the first part of the email is variable $firstPart

SELECT COUNT(*) as num From your_table WHERE LEFT(email,LENGTH(' $firstPart')) = ' $firstPart'

to see how many emails with first part =  $firstPart are in the DB

if there are 0 set $username = $firstPart , if 1 (which username should be  $firstPart1) set the $username = $firstPart2 etc

In other words



if(num ==0) {


$username =  $firstPart;


} else {


$username =  $firstPart.($num+1);


}



(Deventer) #5

You could use it in onBeforeValidate() instead of onBeforeSave()


(Luke Pullin) #6

does anyone have a better example of achieving this; currently i have this;


public function autoUserName($email)

    {

        $screen_name = preg_replace('/([^@]*).*/', '$1', $email);

        $record = User::model()->findByAttributes(array('user_name'=>$user_name));

        $i = 0;

        if ($record === NULL)

        {

            $this->username = preg_replace('/([^@]*).*/', '$1', $email);

        }

     }

Now how do I add 1 to the end of the username if user already exists?


(Luke Pullin) #7

Can a moderator please move this topic to main category so that I get some feedback thanks


(Pestaa) #8



public function autoUserName($email)

{

   $pure_screen_name = $screen_name = strtok($email, '@');

   $i=0;

   while(User::model()->exists('user_name=:test', array(':test'=>$screen_name)))

   {

      $i++;

      $screen_name = $pure_screen_name.$i;

   }


   $this->username = $screen_name;

}



This is capable of adding numbers higher than 1 if the username is taken.

If this doesn’t fit your needs, I’ll move this thread for you.


(Luke Pullin) #9

Perfect pestaa… Champion… very grateful