PHP list sorting

There are times that I have a large list of data which I need to strip out duplicates, and sort. This is really easy to do from the bash shell, but it would be quicker if I had a web interface which would eliminate having to make temporary files and open up a shell window.

Here is the demo of the code.

And here is the source:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
print "<pre>";

function array_trim($a) { $j = 0; for ($i = 0; $i < count($a); $i++) { if ($a[$i] != "") { $b[$j++] = $a[$i]; } }
return $b;
}
function array_diff2($first, $second) {
$res = array();
foreach($first as $key => $val) {
$found = false;
foreach($second as $key2 => $val2) {
if ($val == $val2) {
$found = true;
break;
}
}
if (!$found) {
$res[$key] = $val;
}
}
return $res;
}

if ($_POST){
$nicelist = split("\n",$_POST[input]);
if ($sorttype = "SORT_REGULAR") sort ($nicelist,SORT_REGULAR);
if ($sorttype = "SORT_NUMERIC") sort ($nicelist,SORT_NUMERIC);
if ($sorttype = "SORT_STRING") sort ($nicelist,SORT_STRING);
$nicelist = array_unique ($nicelist);
if ($_POST[removelist]){
#$remove .= "\n";
$remove = split("\n",$_POST[removelist]);
$nicelist = array_merge(array_diff2($nicelist, $remove));
}
$nicelist = implode("\n", $nicelist);
$nicelist = str_replace("\\'","'",$nicelist);
$output = $nicelist;
}
print "</pre>";
?>
<div align="center"><font size="+3" face="Verdana, Arial, Helvetica, sans-serif">Sort
list</font><font face="Verdana, Arial, Helvetica, sans-serif"><br>
<font size="-2">Put anything in Input, and hit submit.<br>
It will be sorted and returned to the output.<br>
Put items to be removed in remove list.</font><br>
&nbsp; </font> </div>
<form name="form1" method="post" action="">
<table width="10%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr align="center" valign="top">
<td>Input / Output:<br> <textarea name="input" cols="35" rows="15" id="input"><?=$output;?></textarea>
<?= $outputcount; ?>values<br>
</td>
<td>&nbsp;&nbsp;&nbsp;</td>
<td>Remove list:<br>
<textarea name="removelist" cols="35" rows="15" id="removelist"><?=$_POST[removelist];?></textarea>
</td>
</tr>
<tr align="center">
<td align="left">Sort method:<br>
<input name="sorttype" type="radio" value="SORT_REGULAR" checked>
compare items normally<br>
<input name="sorttype" type="radio" value="SORT_NUMERIC">
compare items numerically<br>
<input type="radio" name="sorttype" value="SORT_STRING">
compare items as strings</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr align="center">
<td colspan="3">&nbsp;<br> <input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>