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'
用户提供gordon@yourdomain.com,我想创建用户名'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!
希望这是有道理的,看起来像一个基本的功能,但无法在网上任何地方找到它的例子!
9 个解决方案
#1
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.
这不是一个好主意,只需使用他们的完整电子邮件地址。以下电子邮件地址可能是不同的人,但它们将在您的系统下变为相同。
samename@gmail.com
samename@yahoo.com
Adding a number to the end will make the user remember something unique to your system and cause much confusion on their end.
在最后添加一个数字将使用户记住您的系统特有的东西,并在他们的结尾造成很多混乱。
#2
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:
同意,您正在将必要的唯一ID剥离为非唯一ID。除非你想添加某种处理来为用户名或其他东西添加一个数字。如果这是你真正想做的事情,那么应该在电子邮件地址之前设置$ username:
<?php
$username = preg_replace('/([^@]*).*/', '$1', $email);
?>
#3
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 gordon@flash.net to be considered equal to gordon@*.com
重要提示:考虑使用整个电子邮件作为用户名:您不希望gordon@flash.net被视为等同于gordon@*.com
NOTE: example code counts gordon, gordon1, gordon2 but gordonbah, gordonq, gordonxxx too
注意:示例代码计算gordon,gordon1,gordon2但gordonbah,gordonq,gordonxxx也是如此
NOTE: this is pretty rough, and should not be considered best PHP practice; it's just to give the general idea
注意:这非常粗糙,不应被视为最佳PHP实践;它只是给出一般的想法
#4
$username = gordon@example.com;
$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;
}
#5
<?php
preg_match('/[^@]+)@/',$email,$matches);
$username = $matches[1];
check_availability($username);
?>
#6
As of PHP 5.3.0 you can also use:
从PHP 5.3.0开始,您还可以使用:
$email = 'test@*.com';
$user = strstr($email, '@', true);
This will return all the string from the beginning to the first occurrence of '@'. Which in this case is:
这将返回从“@”开始到第一次出现的所有字符串。在这种情况下是:
test
#7
Should suit your needs :D
应该适合您的需求: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;
}
}
#8
I use something like this,
我用这样的东西,
$cust_email = "test@gmail.com";
$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.
如果用户没有在配置文件中设置他/她的名字,我用来设置默认用户名。
#9
You can use strstr
function to find specific word from string
您可以使用strstr函数从字符串中查找特定单词
<?php
$username = strstr("username@domain.com",'@',true); //get text before @
/*
echo strstr("username@domain.com","@"); // get text after @
*/
check_availability($username);
?>
#1
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.
这不是一个好主意,只需使用他们的完整电子邮件地址。以下电子邮件地址可能是不同的人,但它们将在您的系统下变为相同。
samename@gmail.com
samename@yahoo.com
Adding a number to the end will make the user remember something unique to your system and cause much confusion on their end.
在最后添加一个数字将使用户记住您的系统特有的东西,并在他们的结尾造成很多混乱。
#2
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:
同意,您正在将必要的唯一ID剥离为非唯一ID。除非你想添加某种处理来为用户名或其他东西添加一个数字。如果这是你真正想做的事情,那么应该在电子邮件地址之前设置$ username:
<?php
$username = preg_replace('/([^@]*).*/', '$1', $email);
?>
#3
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 gordon@flash.net to be considered equal to gordon@*.com
重要提示:考虑使用整个电子邮件作为用户名:您不希望gordon@flash.net被视为等同于gordon@*.com
NOTE: example code counts gordon, gordon1, gordon2 but gordonbah, gordonq, gordonxxx too
注意:示例代码计算gordon,gordon1,gordon2但gordonbah,gordonq,gordonxxx也是如此
NOTE: this is pretty rough, and should not be considered best PHP practice; it's just to give the general idea
注意:这非常粗糙,不应被视为最佳PHP实践;它只是给出一般的想法
#4
$username = gordon@example.com;
$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;
}
#5
<?php
preg_match('/[^@]+)@/',$email,$matches);
$username = $matches[1];
check_availability($username);
?>
#6
As of PHP 5.3.0 you can also use:
从PHP 5.3.0开始,您还可以使用:
$email = 'test@*.com';
$user = strstr($email, '@', true);
This will return all the string from the beginning to the first occurrence of '@'. Which in this case is:
这将返回从“@”开始到第一次出现的所有字符串。在这种情况下是:
test
#7
Should suit your needs :D
应该适合您的需求: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;
}
}
#8
I use something like this,
我用这样的东西,
$cust_email = "test@gmail.com";
$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.
如果用户没有在配置文件中设置他/她的名字,我用来设置默认用户名。
#9
You can use strstr
function to find specific word from string
您可以使用strstr函数从字符串中查找特定单词
<?php
$username = strstr("username@domain.com",'@',true); //get text before @
/*
echo strstr("username@domain.com","@"); // get text after @
*/
check_availability($username);
?>