如何从提供的电子邮件地址创建用户名 - PHP
我的网站上有一个注册页面,用户只需提供电子邮件地址和密码。
我希望能够使用提供的电子邮件的第一部分自动为此用户创建用户名;
用户提供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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这不是一个好主意,只需使用他们的完整电子邮件地址即可。 以下电子邮件地址可能是不同的人,但在您的系统下它们将变得相同。
在末尾添加数字将使用户记住您的系统所特有的东西,并导致用户产生很多困惑。
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.
Adding a number to the end will make the user remember something unique to your system and cause much confusion on their end.
同意,您正在将必然唯一的 ID 剥离为非唯一 ID。 除非您想添加某种处理以将数字添加到用户名或其他内容。 如果这就是你真正想要做的,应该将 $username 设置为电子邮件地址之前的内容:
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:
比如:
应该做。 您可能想学习正则表达式来完成此类任务。
然后添加计数器:
然后:
重要:考虑使用整个电子邮件作为用户名:您不希望[email protected] 被视为等于[email protected]
注意:示例代码也包含 gordon、gordon1、gordon2,但也包含 gordonbah、gordonq、gordonxxx
注意:这非常粗糙,不应该被视为最佳 PHP 实践; 这只是给出总体思路
Something like:
should do. You may want to learn regular expressions for these kinds of task.
Then you add the counter:
and then:
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
应该适合你的需求:D
Should suit your needs :D
从 PHP 5.3.0 开始,您还可以使用:
这将返回从开头到第一次出现“@”的所有字符串。 在本例中是:
As of PHP 5.3.0 you can also use:
This will return all the string from the beginning to the first occurrence of '@'. Which in this case is:
您可以使用
strstr
函数从字符串中查找特定单词You can use
strstr
function to find specific word from string我使用类似的东西,
如果用户未在配置文件中设置他/她的名字,我会使用它来设置默认用户名。
I use something like this,
I use to set default User Name if user do not set his / her name in the profile.