干净的主机名

发布于 01-06 01:15 字数 372 浏览 1 评论 0原文

我有一个字段要求输入所需的域名,现在有些用户在该字段中包含 www.desireddomain。有了这个值,我的域名注册商 API 将返回错误。我想到的方法是使用 preg_replace() 省略句点(包括句点)之前的任何字符。我有一个

$desired_domain = "www.desireddomain"; // user input
$will_be = "desireddomain"; // final output after preg_replace() ?

而且,这是否将我限制为仅限句点,还是还有什么我应该知道的?谢谢。

I have a field in which I ask for desired domain name, now it came to a point that some user includes www.desireddomain on the field. Having this value, the domain registrar API I have will return an error. The approach I have in mind is to omit any characters before period (including period), using preg_replace(). I have a <select> which contains (.com,.biz,.org,.net)

$desired_domain = "www.desireddomain"; // user input
$will_be = "desireddomain"; // final output after preg_replace() ?

And also, does this limits me to only the period or is there anything more I should know? Thanks.

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

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

发布评论

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

评论(2

晚雾2025-01-13 01:15:46

您不需要使用正则表达式,因为您没有进行“模糊”搜索。如果有一个周期的话,就获取最后一个周期的所有内容。

if ( strrpos('www.domain','.')!==false ) {
    $domain = substr('www.domain',strrpos('www.domain','.')+1);
}

或者,如果您想确保他们没有输入.com、.biz、.org、.net,请使用explode 并进行检查。

$parts = explode('.', 'www.desireddomain.com.net');
$domain = array_pop($parts);
$invalid_parts = array('com','biz','org','net');
if ( in_array($domain, $invalid_parts) && count($parts)>0 ) {
    $domain = array_pop($parts);
} else {
    // No valid domain submitted
}

在此示例中,他们要求使用“com”作为域,即使它无效。您可以创建一个循环,不断从数组中弹出项目,直到找到有效部分或用完项目。

You don't need to use regular expressions since you are aren't doing a "fuzzy" search. Just get everything from the last period on, if there is a period.

if ( strrpos('www.domain','.')!==false ) {
    $domain = substr('www.domain',strrpos('www.domain','.')+1);
}

Alternatively, if you want to make sure they didn't enter .com,.biz,.org,.net, use explode and do a check.

$parts = explode('.', 'www.desireddomain.com.net');
$domain = array_pop($parts);
$invalid_parts = array('com','biz','org','net');
if ( in_array($domain, $invalid_parts) && count($parts)>0 ) {
    $domain = array_pop($parts);
} else {
    // No valid domain submitted
}

In this example, they are asking to use "com" as the domain, even though it's no valid. You could create a loop instead, where you continuously pop items off the array until you find a valid part or run out of items.

乖不如嘢2025-01-13 01:15:46

使用正则表达式 /^.*\./,如下所示:

$desired_domain = "www.desireddomain";
$will_be = preg_replace("/^.*\./","",$desired_domain);
print $will_be; //Outputs "desireddomain"

但是请记住,用户可以输入多种无效数据。您最好告诉用户设置一组限制,然后根据正则表达式检查它们是否有效。

Use the regular expression /^.*\./, as in:

$desired_domain = "www.desireddomain";
$will_be = preg_replace("/^.*\./","",$desired_domain);
print $will_be; //Outputs "desireddomain"

Keep in mind, however, that there are plenty of varieties of invalid data that the user could type in. You are better off telling the user a set of restrictions and then checking them against a regular expression for a valid input.

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