PHP解析输入域名的textarea(以空格、逗号、换行符分隔)

发布于 2024-08-03 04:19:36 字数 387 浏览 2 评论 0原文

对于我的用户,我需要呈现一个屏幕,他们可以在文本区域中输入多个域名。用户可以将域名放在不同的行上,或者用空格或逗号分隔它们(甚至可能是分号 - 我不知道!)

我需要解析和识别具有扩展名的各个域名(这将是 .com,其他任何域名都可以)被忽略)。

用户输入可以是:

asdf.com

qwer.com

AND/OR

wqer.com, gwew.com

AND/OR

ertert.com gdfgdf.com

没有人会输入像 www.abczone.com 这样的 3 级域名,但如果他们这样做我只对提取 abczone.com 部分感兴趣。 (我可以有一个单独的正则表达式来验证/提取每个正则表达式)。

For my users I need to present a screen where they can input multiple domain names in a textarea. The users can put the domain names on different lines, or separate them by spaces or commas (maybe even semicolons - I dont know!)

I need to parse and identify the individual domain names with extension (which will be .com, anything else can be ignored).

User input can be as:

asdf.com

qwer.com

AND/OR

wqer.com, gwew.com

AND/OR

ertert.com gdfgdf.com

No one will input a 3 level domain like www.abczone.com, but if they do I'm only interested in extracting the abczone.com part. (I can have a separate regex to verify/extract that from each).

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

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

发布评论

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

评论(2

农村范ル 2024-08-10 04:19:36

这将做到这一点:

(\b[a-zA-Z][a-zA-Z0-9-]*)(?=\.com\b)

“查找一个字母的所有序列,后跟字母、数字或连字符,后跟.com,然后是一个单词分隔符。”

(您需要最后一点来防止从 bim.command.com 获取 bim.com。)

Python 测试用例,因为我没有 PHP 测试环境手:

DATA = "asdf.com\nx-123.com, gwew.com bim.command.com 123.com, x_x.com"
import re
print re.findall(r'(\b[a-zA-Z][a-zA-Z0-9-]*)(?=\.com\b)', DATA)
# Prints ['asdf', 'x-123', 'gwew', 'command']

This will do it:

(\b[a-zA-Z][a-zA-Z0-9-]*)(?=\.com\b)

"Find all sequences of a letter followed by letters, digits, or hyphens, followed by .com then a word break."

(You need the last bit to protect against picking up bim.com from bim.command.com.)

Python test case because I don't have a PHP test environment to hand:

DATA = "asdf.com\nx-123.com, gwew.com bim.command.com 123.com, x_x.com"
import re
print re.findall(r'(\b[a-zA-Z][a-zA-Z0-9-]*)(?=\.com\b)', DATA)
# Prints ['asdf', 'x-123', 'gwew', 'command']
八巷 2024-08-10 04:19:36

在这里,如果您愿意,可以使用 i 修饰符并删除所有大写 AZ:

\b([a-zA-Z][0-9a-zA-Z\-]{1,62})\.com\b

Here it is, you can use the i modifier and delete all the uppercase A-Z if you want to:

\b([a-zA-Z][0-9a-zA-Z\-]{1,62})\.com\b
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文