Yii Framework Forum: Extract part of string - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Extract part of string Rate Topic: -----

#1 User is offline   ooaat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 01-May 09

Posted 05 June 2009 - 11:17 AM

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


0

#2 User is offline   Spyros 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 461
  • Joined: 14-April 09
  • Location:Athens - Greece

Posted 05 June 2009 - 11:36 AM

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
0

#3 User is offline   ooaat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 01-May 09

Posted 05 June 2009 - 12:01 PM

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


0

#4 User is offline   Spyros 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 461
  • Joined: 14-April 09
  • Location:Athens - Greece

Posted 05 June 2009 - 02:48 PM

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);
}

0

#5 User is offline   Dave 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 187
  • Joined: 09-October 08

Posted 30 June 2009 - 12:05 PM

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

#6 User is offline   ooaat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 01-May 09

Posted 04 September 2009 - 03:03 PM

View PostSpyros, on 05 June 2009 - 02:48 PM, said:

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
<pre class='prettyprint'>
if(num ==0) {
$username =  $firstPart;
} else {
$username =  $firstPart.($num+1);
}
</pre>


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?
0

#7 User is offline   ooaat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 01-May 09

Posted 05 September 2009 - 01:09 AM

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

#8 User is offline   pestaa 

  • past Yii dev member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 705
  • Joined: 07-May 09
  • Location:Hungary

Posted 05 September 2009 - 04:50 AM

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.
0

#9 User is offline   ooaat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 01-May 09

Posted 05 September 2009 - 10:10 AM

Perfect pestaa... Champion... very grateful
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users