I really don’t understand regular expressions and was wondering what the following regular expressions do. I want my address and name to accept ., - and alphanumerics.
Will this work or is there need for improvement? Plus if someone can break down the regular expressions '/^[A-Z0-9 \'.-]{1,255}$/i' so I can understand every part better.
Here is the php code.
if (preg_match ('/^[A-Z0-9 \'.-]{1,255}$/i', $_POST['address'])) {
$address = mysqli_real_escape_string($mysqli, htmlentities($_POST['address']));
} else {
echo '<p class="error">Please enter your address!</p>';
}
if (preg_match ('/^[A-Z0-9 \'.-]{1,255}$/i', $_POST['name'])) {
$name = mysqli_real_escape_string($mysqli, htmlentities($_POST['name']));
} else {
echo '<p class="error">Please enter your name!</p>';
}
Your pattern basically allowes any combination (between 1 and 255 characters) of the following: A-Z, 0-9, space, \, ‘, .
Decide for yourself if this is good enough.
As for your pattern:
The i at the end means it isnt case sensitive
The slashes denote the beginning and the end of the pattern
The ^ is the beginning and $ is the end of the string you look for
This allowes and combination of the characters between the brackets with 1 to 255 repetitions