如何从提供的电子邮件地址创建用户名 - PHP

发布于 2024-07-24 12:40:18 字数 269 浏览 6 评论 0原文

我的网站上有一个注册页面,用户只需提供电子邮件地址和密码。

我希望能够使用提供的电子邮件的第一部分自动为此用户创建用户名;

用户提供gordon@yourdomain.com,我想将用户名设为“gordon”,

我不需要解释如何创建表单或向数据库提交数据,只需从提供的电子邮件中提取数据的代码,如有必要,如果出现重复,请在末尾添加数字。

希望这是有道理的,似乎是一个基本功能,但在网络上的任何地方都找不到它的示例!

I have a signup page on my website where a user must provide a email address and password only.

I want to be able to create a username for this user automatically by using the first part of the email provided;

User supplies gordon@yourdomain.com, i want to make username 'gordon'

I don't need explanation on how to create form or submission of data to database, just the code to extract data from email provided, and if necessary, if duplicate occurs add number to end.

Hope this makes sense, seems like a basic function but couldn't find examples of it anywhere on net!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

你的呼吸 2024-07-31 12:40:18

这不是一个好主意,只需使用他们的完整电子邮件地址即可。 以下电子邮件地址可能是不同的人,但在您的系统下它们将变得相同。

[email protected]
[email protected]

在末尾添加数字将使用户记住您的系统所特有的东西,并导致用户产生很多困惑。

This is not a good idea, just use their full email address. The following email addresses could be different people, but they will become the same under your system.

[email protected]
[email protected]

Adding a number to the end will make the user remember something unique to your system and cause much confusion on their end.

雨后彩虹 2024-07-31 12:40:18

同意,您正在将必然唯一的 ID 剥离为非唯一 ID。 除非您想添加某种处理以将数字添加到用户名或其他内容。 如果这就是你真正想要做的,应该将 $username 设置为电子邮件地址之前的内容:

<?php
   $username = preg_replace('/([^@]*).*/', '$1', $email);
?>

Agreed, you're stripping a necessarily unique ID into a non-unique ID. Unless you want to add some sort of handling to add a number to the username or something. If that's what you really want to do, this should set $username to the stuff before the email address:

<?php
   $username = preg_replace('/([^@]*).*/', '$1', $email);
?>
耳钉梦 2024-07-31 12:40:18

比如:

$username = left($email, stripos($email, '@'));

应该做。 您可能想学习正则表达式来完成此类任务。

然后添加计数器:

function countOccurrences($name)
{
    $con = mysql_connect(___, ___, ___);
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db(___, $con);

    $result = mysql_query("
        SELECT COUNT(*) AS countOccurrences
        FROM users
        WHERE username LIKE '" . mysql_real_escape_string($name, $con) . "%'
    ");

    $row = mysql_fetch_array($result);

    $number = $row['countOccurrences'];

    mysql_close($con);

    return $number;

}

然后:

$countUsers = countOccurrences($username);
if ($countUsers>0)    
{    
      $username = $username . $countUsers;
}

重要:考虑使用整个电子邮件作为用户名:您不希望[email protected] 被视为等于[email protected]

注意:示例代码也包含 gordon、gordon1、gordon2,但也包含 gordonbah、gordonq、gordonxxx

注意:这非常粗糙,不应该被视为最佳 PHP 实践; 这只是给出总体思路

Something like:

$username = left($email, stripos($email, '@'));

should do. You may want to learn regular expressions for these kinds of task.

Then you add the counter:

function countOccurrences($name)
{
    $con = mysql_connect(___, ___, ___);
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db(___, $con);

    $result = mysql_query("
        SELECT COUNT(*) AS countOccurrences
        FROM users
        WHERE username LIKE '" . mysql_real_escape_string($name, $con) . "%'
    ");

    $row = mysql_fetch_array($result);

    $number = $row['countOccurrences'];

    mysql_close($con);

    return $number;

}

and then:

$countUsers = countOccurrences($username);
if ($countUsers>0)    
{    
      $username = $username . $countUsers;
}

IMPORTANT: Consider using the whole email as username: you don't want [email protected] to be considered equal to [email protected]

NOTE: example code counts gordon, gordon1, gordon2 but gordonbah, gordonq, gordonxxx too

NOTE: this is pretty rough, and should not be considered best PHP practice; it's just to give the general idea

没企图 2024-07-31 12:40:18

应该适合你的需求:D

$username = substr($username, 0, strpos($username, '@'));

$username = mysql_real_escape_string($username);

$result = mysql_query('SELECT `username` FROM `users` WHERE `username` = \'' . $username . '%\';');

if (mysql_num_rows($result) > 0) {

   $i = 0;

   while ($name_arr = mysql_fetch_assoc($result)) {

      $name = $name_arr['username'];       

      $after = substr($name, strlen($username));

      if (ctype_digit($after)) {

         if (($after = (int) $after) > $i) {

            $i = $after;

         }

      }

   }

   if ($i > 0) {
      $username .= $i;
   }

}

Should suit your needs :D

$username = substr($username, 0, strpos($username, '@'));

$username = mysql_real_escape_string($username);

$result = mysql_query('SELECT `username` FROM `users` WHERE `username` = \'' . $username . '%\';');

if (mysql_num_rows($result) > 0) {

   $i = 0;

   while ($name_arr = mysql_fetch_assoc($result)) {

      $name = $name_arr['username'];       

      $after = substr($name, strlen($username));

      if (ctype_digit($after)) {

         if (($after = (int) $after) > $i) {

            $i = $after;

         }

      }

   }

   if ($i > 0) {
      $username .= $i;
   }

}
别在捏我脸啦 2024-07-31 12:40:18
$username = [email protected];

$username_arr = explode('@',$username);

$username = $username_arr[0];    

if( !is_taken( $username ) )
{
    //The name is not taken.
}
else
{
    //The name is taken, add numbers and do the search again.
}

function is_taken( $username )
{   
    $db_link = mysql_connect($host,$user,$pass) or die('Could not connect to database');

    $username = mysql_real_escape_string( $username );

    $sql = "SELECT * FROM `database`.`users` WHERE `username` = '$username'";

    $res = mysql_query( $sql , $db_link );

    return mysql_num_rows( $res ) == 1;

}
$username = [email protected];

$username_arr = explode('@',$username);

$username = $username_arr[0];    

if( !is_taken( $username ) )
{
    //The name is not taken.
}
else
{
    //The name is taken, add numbers and do the search again.
}

function is_taken( $username )
{   
    $db_link = mysql_connect($host,$user,$pass) or die('Could not connect to database');

    $username = mysql_real_escape_string( $username );

    $sql = "SELECT * FROM `database`.`users` WHERE `username` = '$username'";

    $res = mysql_query( $sql , $db_link );

    return mysql_num_rows( $res ) == 1;

}
不乱于心 2024-07-31 12:40:18
<?php
 preg_match('/[^@]+)@/',$email,$matches);
 $username = $matches[1];
 check_availability($username);
?>
<?php
 preg_match('/[^@]+)@/',$email,$matches);
 $username = $matches[1];
 check_availability($username);
?>
明月夜 2024-07-31 12:40:18

从 PHP 5.3.0 开始,您还可以使用:

$email = '[email protected]';
$user = strstr($email, '@', true);

这将返回从开头到第一次出现“@”的所有字符串。 在本例中是:

test

As of PHP 5.3.0 you can also use:

$email = '[email protected]';
$user = strstr($email, '@', true);

This will return all the string from the beginning to the first occurrence of '@'. Which in this case is:

test
红ご颜醉 2024-07-31 12:40:18

您可以使用 strstr 函数从字符串中查找特定单词

   <?php

    $username = strstr("[email protected]",'@',true); //get text before @    
    /*    
    echo strstr("[email protected]","@"); // get text after @
    */
     check_availability($username);

    ?>

You can use strstr function to find specific word from string

   <?php

    $username = strstr("[email protected]",'@',true); //get text before @    
    /*    
    echo strstr("[email protected]","@"); // get text after @
    */
     check_availability($username);

    ?>
醉梦枕江山 2024-07-31 12:40:18

我使用类似的东西,

$cust_email = "[email protected]";
$parts = explode("@", $cust_email);
$cust_name = $parts[0];

如果用户未在配置文件中设置他/她的名字,我会使用它来设置默认用户名。

I use something like this,

$cust_email = "[email protected]";
$parts = explode("@", $cust_email);
$cust_name = $parts[0];

I use to set default User Name if user do not set his / her name in the profile.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文