<?php
main("Regular Expression Tester");

// Control the operation of a page
function main($title = "") {
    $msg = "";
    $pattern = "/^She/";
    $subject = "She sells sea shells";
    if (isset($_POST["submit"])) {
        $pattern = $_POST["pattern"];
        if (get_magic_quotes_gpc()) {
            $pattern = stripslashes($pattern);
        }
        $subject = $_POST["subject"];
        @$found = preg_match($pattern, $subject);
        if ($found) {
            $msg = "Matches";
        } else {
            $msg = "No match";
        }
    }
    showContent($title, $msg, $pattern, $subject);
}

// Display the content of a page
function showContent($title, $msg, $pattern, $subject) {
    echo<<<HTML
<html>
<head><title>$title</title></head>
<body>
<h1>$title</h1>
<p>Enter a regular expression in <b>Pattern</b>
<br>and the string to search in <b>Subject</b>
<br>and then press the <b>Test</b> button.</p>

<form method="POST" action="/cis165/04s/examples/regex.php">
<p>Pattern: <input type="text" name="pattern" value="$pattern" size="40">
<p>Subject: <input type="text" name="subject" value="$subject" size="40">
<p>$msg</p>
<p><input type="submit" name="submit" value="Test"></form>
</body>
</html>

HTML;
}
?>