<?php

if (isset($_GET['towns']) && isset($_GET['townsToExclude'])) {
    $towns = htmlspecialchars($_GET['towns']);
    $towns = explode("\n", $towns);
    $towns = array_map('trim', $towns);
    $excludeTowns = htmlspecialchars($_GET['townsToExclude']);
    $excludeTowns = explode("\n", $excludeTowns);
    $excludeTowns = array_map('trim', $excludeTowns);
    $result = arrayDiff($towns, $excludeTowns);
    printAsList($result);
}
function printAsList(array $arr)
{
    echo "<ul>\n";
    foreach ($arr as $item) {
        echo "<li>{$item}</li>\n";
    }
    echo "</ul>\n";
}
function arrayDiff(array $array, array $arrToExcl)
{
    $result = [];
    foreach ($array as $item) {
        if (!in_array($item, $arrToExcl)) {
            $result[] = $item;
        }
    }
    return $result;
}
?>
Esempio n. 2
0
<body>
<?php 
function excludeFromArray(array $arr, array $excludeArr) : array
{
    $result = [];
    foreach ($arr as $item) {
        if (!in_array($item, $excludeArr)) {
            $result[] = $item;
        }
    }
    return $result;
}
function printAsList(array $arr)
{
    echo "<ul>\n";
    foreach ($arr as $item) {
        echo "<li>{$item}</li>\n";
    }
    echo "</ul>\n";
}
if (isset($_GET['towns']) && isset($_GET['townsToExclude'])) {
    $towns = array_map('trim', explode("\n", $_GET['towns']));
    $townsToExclude = array_map('trim', explode("\n", $_GET['townsToExclude']));
    $remainingTowns = excludeFromArray($towns, $townsToExclude);
    printAsList($remainingTowns);
} else {
    echo "<form>\n    <div style=\"display: inline-block\">\n        <div>Towns</div>\n        <textarea rows=\"10\" name=\"towns\"></textarea>\n    </div>\n    <div style=\"display: inline-block\">\n        <div>Towns to exclude</div>\n        <textarea rows=\"10\" name=\"townsToExclude\"></textarea>\n    </div>\n    <div><input type=\"submit\" value=\"Exclude\"></div>\n</form>";
}
?>
</body>
</html>