使用前缀/后缀生成 URL 名称






2.15/5 (6投票s)
使用前缀/后缀生成 URL 名称
更新时间
本文已更新。有人建议检查域名的可用性。此功能现已包含在内。我最初的目的是让该程序作为记忆辅助或营销工具。
我无意让程序的建议是最终的;我原本的意图是让这些词汇能够激发创造性思维。但是,读者似乎更倾向于完全采纳建议的词列表。因此,我响应了这一需求。
引言
计算机非常适合处理枯燥的重复性任务。提供建议的 URL 名称列表就是一个很好的例子。
背景
Web 设计师通常知道他们想要的 URL 名称的基本部分,但最常见的名称可能已经被注册。最简单的绕过方法是提供一个时髦的前缀或后缀词,用于品牌推广。该程序采用用户存储在程序中的列表,并打印出所有 组合。
假设我们有一个客户是做婚礼蛋糕的。如果我们提供“Cake”,我们的建议列表 可能包括“LaserCake”、“MaxiCake”、“QuickCake”、“SuperCake”、“CakeMan”、“CakeHut”等。该程序的美妙之处在于我们可以快速尝试几个基本名称,并看到数百甚至数千种组合的列表。
例如,我们可能想尝试“Cake”、“Frosted”、“Food”、“Party”、“Desert”,看看会产生哪些有趣的组合。
大多数 商业域名提供商确实提供某种形式的域名建议。但它们有几个重要的限制。建议可能包括购买者的姓名。这对于为客户寻找名称的人来说是不合适的。此外,建议缺乏对我们可能有的偏好的智能。该程序允许我们对此进行调整。
第二个问题是域名很大程度上受到其代表的行业的 Thus, I have responded to that need. 如果客户与划船或水上运动相关,域名提供商可能不会包含带有“wet”、“ski”、“water”、“hydro”或“wave”的建议。
我为每个主要行业制作此程序的副本。例如,我有一个用于“景观或房地产”词汇的程序,另一个有“工程和技术”风格的词汇,等等。我发现大约 50 个前缀和 100 个针对特定行业的后缀可以产生足够的建议,其中一个会给我一个非常好的 URL 名称主意:)
第一步:简单的 URL 名称表单
对于这个应用程序,我们将使用 PHP。该应用程序可以放置在网站上,也可以从我们的“本地”Web 开发环境(如 WAMP、MAMP 或 XAMP)中使用。
<?php
/*
+--------------------------------------------------------------------+
| file: url_names.php version: 2012-05-01 |
| by: Jan Zumwalt - NeatInfo.com |
| copyright: GNU GENERAL PUBLIC LICENSE |
| www.gnu.org/copyleft/gpl. |
| purpose: User supplies web name, program adds prefix and |
| suffix words from array list. |
+--------------------------------------------------------------------+ */
// make things pretty using dark theme
echo"
<head>
<style>
body {
font-size : 12 pt;
font-family : arial;
color : #ffb; /* yellow font color */
background-color : #123; /* dark navy bk ground */
line-height : 12pt;
margin : 75px;
}
</style>
</head>
<body>
";
<!-- code to be added -->
// +-------------------- show input form ---------------------+
echo "
<form name='inputform' action=' " . $_SERVER['PHP_SELF'] . " ' method=\"get\">
URL Name: <input type='text' width='100' maxlength='100' name='name' />
<input type='submit' value='Submit' />
<INPUT type='reset' />
<br><br><b><span style='color:#e33;'>Be patient, this may take several minutes!</span></b>
</form>
";
?>
我们从一个简单的 CSS 有吸引力的主题和 基本的 HTML 表单开始,该表单允许用户输入建议的 URL 名称。在此示例中,我们假设我们将为从事烹饪、餐饮或烘焙行业的人员设置一个网站。
您会注意到,当按下提交按钮时,表单使用 php $_SERVER['PHP_SELF'] 来调用自身。用户输入的单词作为变量传递在 $_GET['name'] 中。
此时我们可以运行该程序,但它不会做任何事情,因为正在传递的变量没有被处理。
第二步:将前缀和后缀词存储在数组中
接下来,我们存储将添加到 URL 名称的前缀 和后缀词列表。
现在我必须承认,我提供的单词列表非常通用。我认为我个人的单词列表是一种商业秘密。但是,仔细思考将使您能够极大地扩展这个列表。例如,我认为“Web”、“Fire”和“Hot”这样的词是很好的前缀。好的后缀包括“Wiz”、“Man”和“Mart”。
// words to be added at the beginning
$prefix = array(
"Cool",
"Fast",
"Fire",
"Fry",
"Hot",
"Maxi",
"Quick",
"Sugar",
"Super",
"Sweet",
"Web",
"X",
"Xtra"
);
// words to be added at the end
$suffix = array(
"Dog",
"Hut",
"Kid",
"Kit",
"Man",
"Power",
"Pro",
"Shop",
"Shot",
"Store",
"Wiz",
"World"
);
数组中的每个单词都将被回显到浏览器并显示。
现在我们需要添加代码来处理用户的输入(如果存在)。我们检查用户是否提交了 URL 名称。它将通过 $_GET['name'] 变量传递,并且不应为空。
第三步:处理用户输入并添加前缀/后缀
接下来,我们处理数组中提供的prefix或suffix词列表,并将其打印出来。我们使用循环一次处理一个数组中的每个单词。我们还使用 PHP checkdnsrr($name, $type)
函数来测试和查看域名是否已注册。如果域名未注册,则 .com 扩展名以绿色字母显示。为了保持整洁,我们使用 HTML 表格来格式化输出。
/* +----------------------------------+
| process PREFIX |
+----------------------------------+ */
echo "<br><span style='color:#e33;'><h1>Prefix</h1></span>"; // word list title
echo "<table>";
foreach($prefix as $item) { // add each prefix and show
echo "<tr><td>" . $item . $urlname . "</td>";
// check if .COM domain name exists
$name = $item . $urlname . ".com";
$type = "ANY"; // domain type may be any of: A, MX, NS, SOA, PTR, CNAME, AAAA, A6, SRV, NAPTR, TXT or ANY.
if (checkdnsrr($name, $type)) {
echo "<td><span style='color:#e33;'>[.com]</span></td>"; // this is a registered domain
} else {
echo "<td><span style='color:#6fa;'>[.com]</span></td>"; // this domain doesn't exist!
} // end of domain check
echo "</tr>";
} // end of each prefix
echo "</table>";
上面的示例显示了如何处理前缀。后缀以相同的方式处理。
第四步:整合
让我们将所有代码与前缀和后缀部分一起整合起来。
<?php
/*
+--------------------------------------------------------------------+
| file: url_names.php version: 2012-05-01 |
| by: Jan Zumwalt - NeatInfo.com |
| copyright: GNU GENERAL PUBLIC LICENSE |
| https://gnu.ac.cn/copyleft/gpl.html |
| |
| purpose: User supplies web name, program adds prefix and |
| suffix words from array list. |
+--------------------------------------------------------------------+ */
// make things pretty using dark theme
echo"
<head>
<style>
body {
font-size : 12 pt;
font-family : arial;
color : #ffb; /* yellow font color */
background-color : #123; /* dark navy bk ground */
line-height : 12pt;
margin : 75px;
}
</style>
</head>
<body>
";
// list of prefix words to be added to beginning of URL name
$prefix = array(
"Cool",
"Fast",
"Fire",
"Fry",
"Hot",
"Maxi",
"Quick",
"Sugar",
"Super",
"Sweet",
"Web",
"X",
"Xtra"
);
// list of suffix words to be added to end of URL name
$suffix = array(
"Dog",
"Hut",
"Kid",
"Kit",
"Man",
"Power",
"Pro",
"Shop",
"Shot",
"Store",
"Wiz",
"World"
);
// process form, show prefix and suffix word combinations
if (isset($_GET['name']) && $_GET['name'] != "") {
$urlname = ucfirst($_GET['name']); // first letter of "name" to uppercase, test -> Test
ini_set('max_execution_time', 300); // (5min) over ride default 30sec timeout error
/* +----------------------------------+
| process PREFIX |
+----------------------------------+ */
echo "<br><span style='color:#e33;'><h1>Prefix</h1></span>"; // word list title
echo "<table>";
foreach($prefix as $item) { // add each prefix and show
echo "<tr><td>" . $item . $urlname . "</td>";
// check if .COM domain name exists
$name = $item . $urlname . ".com";
$type = "ANY"; // domain type may be any of: A, MX, NS, SOA, PTR, CNAME, AAAA, A6, SRV, NAPTR, TXT or ANY.
if (checkdnsrr($name, $type)) {
echo "<td><span style='color:#e33;'>[.com]</span></td>"; // this is a registered domain
} else {
echo "<td><span style='color:#6fa;'>[.com]</span></td>"; // this domain doesn't exist!
} // end of domain check
echo "</tr>";
} // end of each prefix
echo "</table>";
/* +----------------------------------+
| process SUFFIX |
+----------------------------------+ */
echo "<br><span style='color:#e33;'><h1>Suffix</h1></span>"; // word list title
echo "<table>";
foreach($suffix as $item) { // add each prefix and show
echo "<tr><td>" . $urlname . $item . "</td>";
// check if .COM domain name exists
$name = $urlname . $item . ".com";
$type = "ANY"; // domain type may be any of: A, MX, NS, SOA, PTR, CNAME, AAAA, A6, SRV, NAPTR, TXT or ANY.
if (checkdnsrr($name, $type)) {
echo "<td><span style='color:#e33;'>[.com]</span></td>"; // this is a registered domain
} else {
echo "<td><span style='color:#6fa;'>[.com]</span></td>"; // this domain doesn't exist!
} // end of domain check
echo "</tr>";
} // end of each suffix
echo "</table>";
echo "<br><span style='color:#6fa;'>Green = name available</span><br>";
} // end of process form
// +-------------------- show input form ---------------------+
echo "
<form name='inputform' action=' " . $_SERVER['PHP_SELF'] . " ' method=\"get\">
URL Name: <input type='text' width='100' maxlength='100' name='name' />
<input type='submit' value='Submit' />
<INPUT type='reset' />
<br><br><b><span style='color:#e33;'>Be patient, this may take several minutes!</span></b>
</form>
";?>
进一步
在原始文章中,我没有进行域名注册检查,但一些读者表示反对。进行域名检查的缺点是每个域名至少需要一秒钟。如果您正在检查 100 个名称,域名检查将近乎两分钟的处理时间。
无论如何,用户很容易注释掉域名检查。还有人建议检查其他扩展名,例如 .org、.biz 等。很明显,如果对大量名称执行此操作,程序将太慢。
本文的目标是将主题保持在初学者水平。不乏可以进行的增强。例如,与其让程序检查域名可用性,不如为用户提供一个可以选择的按钮。这肯定会解决计算速度慢的问题。
但是,包含域名检查按钮需要额外的例程和打印输出。这几乎会使代码量加倍,并且肯定会吓到新程序员。鼓励用户在需要时添加此功能。也许在未来的更新中,我可以展示如何实现此选项。
结论
在我开发此实用程序时,我在 2-3 小时内提交了该表单 15-20 次。我注意到我的域名可用性请求开始被拒绝。我不得不继续到第二天。域名服务器似乎足够智能,能够看到我正在发出的请求数量很大。因此,请注意,在给定时间内可能存在对请求数量的某些限制。
希望这对您有帮助!