3
\$\begingroup\$

I'm looking for feedback about anything and everything to do with this short program. For example, is there a risk in not sanitising the data, and if so, how would I do that given the need for non-alpha-numerics? How is the logic? Is my html up to date? Suggestions for styling? All input appreciated. I'm on a learning curve to become a professional, so need to be producing quality code.

<?php
/*
PHP Regex tester
Author: Robin Andrews 2016
*/

// Has form been reset?
if (!empty($_POST['reset'])){
    $_POST = array();
    $msg = "";
}

// Check if fields completed and process if so
if (!empty($_POST['pattern']) && !empty($_POST['string'])){
    $pattern = $_POST['pattern'];
    $string = $_POST['string'];

    if (preg_match_all('~'.$pattern.'~', $string, $matches)){
        $groups = implode("; ", $matches[0]);
        $count = count($matches[0]);
        $msg =  $count . " Matches present: " . $groups;
    } else {
        $msg = "Pattern is not present";
    }

    // clear $_POST data
    $_POST = array();

}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Regex Tester</title>
    <style>
        body{
            font-family: arial;
            font-size: 18px;
        }
        #wrapper{
            width: 400px;
            margin: 20px auto;
            border: 2px solid blue;
        }

        h3{
            text-align: center;
        }

        input, textarea{
            font-size: inherit;
            font-family: inherit;
            box-sizing: border-box;
            margin: 5px;
            width:300px;
            border-radius: 10px;
            padding: 5px;
        }

        form{
            width: 300px;
            margin: 5px auto;
        }

        input[type=submit], #reset{
            font-family: inherit;
            font-size: 16px;
            box-sizing: border-box;
            margin: 5px 0;
            width:100px;
            border-radius: 10px;
            padding: 2px;
        }

        #submit{
            margin-left:5px;
        }

        #message{
            margin: 10px;
        }

    </style>
</head>
<body>

    <div id="wrapper">
        <h3>Regex Tester</h3>
        <form action="" method="post">
            Enter a string: <textarea name="string" rows="4" autocomplete="off"><?=isset($string)? $string : ''?></textarea><br>
            Enter a pattern: <input type="text" name="pattern" autocomplete="off" value="<?=isset($pattern)? $pattern : ''?>"><br>
            <input type="submit" value="submit" id="submit">
            <input type="submit" value="reset" id="reset" name="reset">
        </form>
        <div id="message">
            <p><?=isset($msg)? $msg : '&nbsp'?></p>
        </div><!-- message -->
    </div><!--wrapper-->
</body>
</html>
\$\endgroup\$
  • \$\begingroup\$ What if $pattern has ~ character? \$\endgroup\$ – hjpotter92 Jun 12 '16 at 14:43
1
\$\begingroup\$

If you don't want to extend this site and you will not change the current page at all, then your code is completely ok. But you probably will therefore you need to make it more flexible.

  1. Put css style and php code to different files. (see require_once)
  2. Build a class to sanitize _POST data and provide necessary data (and the reset mechanism)
  3. Build a class for executing preg_match_all() and processing the output (use spl-autoload for your classes)

The next step is to learn the MVC pattern...

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.