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 : ' '?></p>
</div><!-- message -->
</div><!--wrapper-->
</body>
</html>
$pattern
has~
character? \$\endgroup\$