Skip to content

Commit 9ef028c

Browse files
committed
Adds getsubs script
1 parent d8f885f commit 9ef028c

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

remove-subdomains/getsubs.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env php
2+
<?php
3+
$suffixes = @file('suffixes.txt');
4+
if (!$suffixes) die('failed to load suffixes.txt');
5+
6+
$suffixes = array_map(function($line){
7+
return trim($line, " \t\n\r\0\x0B.");
8+
}, $suffixes);
9+
10+
$suffixes = array_filter($suffixes, function($line){
11+
return $line != "";
12+
});
13+
14+
$map = [];
15+
16+
foreach ($suffixes as $suffix){
17+
$map[$suffix] = true;
18+
}
19+
20+
while (($line = fgets(STDIN)) != false){
21+
$candidate = trim($line);
22+
$parts = explode(".", $candidate);
23+
24+
$tail = [];
25+
array_unshift($tail, array_pop($parts));
26+
27+
if (isset($map[$tail[0]])){
28+
// We've got a tld, try for an sld
29+
array_unshift($tail, array_pop($parts));
30+
31+
if (!isset($map[implode(".", $tail)])){
32+
// No match, put it back
33+
array_push($parts, array_shift($tail));
34+
}
35+
}
36+
37+
array_pop($parts);
38+
//echo array_pop($parts).".".implode(".", $tail).PHP_EOL;
39+
echo implode(".", $parts).PHP_EOL;
40+
}

0 commit comments

Comments
 (0)