I have this code in preg_match
if (preg_match("/(for+\([\w\-]+\;[\w\-]+\;[\w\-]+\){)/",$email))
{
$message = "Valid input";
}
else
$message ="Invalid Input";
if the user will input for(aw;aw;aw){
if will output Valid input
but if the user will put a space like for (awd ; awd; awd) {
it will output invalid input..
my problem is how can i bypass space or remove space without using explode to my string..
need help..
You can match a space like any other character. So for example, you can just add spaces where needed, like below:
if (preg_match("/(for+ *\([\w\-]+ *\; *[\w\-]+ *\; *[\w\-]+\) *{)/",$email))
However, for+ matches 1 or more literal r's so would also match positively on forrrr, so just using for might be more appropriate there.
Related
I would like to validate a string with a pattern that can only contain letters (including letters with accents). Here is the code I use and it always returns "nok".
I don't know what I am doing wrong, can you help? thanks
$string = 'é';
if(preg_match('/^[\p{L}]+$/i', $string)){
echo 'ok';
} else{
echo 'nok';
}
Add the UTF-8 modifier flag (u) to your expression:
/^\p{L}+$/ui
There is also no need to wrap \p{L} inside of a character class.
I don't know if this helps anybody that will check this question / thread later. The code below allows only letters, accents and spaces. No symbols or punctuation like .,?/>[-< etc.
<?php
$string = 'États unis and états unis';
if(preg_match('/^[a-zA-Z \p{L}]+$/ui', $string)){
echo 'ok';
} else{
echo 'nok';
}
?>
If you want to add numbers too, just add 0-9 immediately after Z like this a-zA-Z0-9
Then if you are applying this to form validation and you are scared a client/user might just hit spacebar and submit, just use:
if (trim($_POST['forminput']) == "") {... some error message ...}
to reject the submission.
I'm using regex (I think that's what it's called - haha) to check my users' name to make sure it's valid. I want to make sure that the user doesn't have any characters. Just letters and spaces. I've got the only letters part down, but I can't get the spaces part fixed.
Here's what I'm using now..
if(preg_match("/[^a-zA-Z]/", " ", $name) != 0) {
$errorlist = $errorlist."<li>You must enter a valid First and Last name (check for invalid characters)</li>";
}
Anyone see what I'm doing wrong?
Just add a space to your regex pattern. PS - your second parameter for preg_match() should probably be $name, right? Is there a reason you were testing " "?
if(preg_match("/[^a-zA-Z ]/", $name) != 0) {
$errorlist = $errorlist."<li>You must enter a valid First and Last name (check for invalid characters)</li>";
}
For my PHP script I have this code:
if (!preg_match("/[^A-Za-z]/", $usersurname))
$usersurname_valid = 1;
This worked untill I realized a surname can be two or more words... doh.
Anyone can tell me how to write this code if I want to allow 1 space between two worlds? For example:
Jan Klaas is now wrong and Jan Klaas should be allowed, also Jan Klaas Martijn and so on should be allowed.
Even better would be a preg replace, to replace two or more spaces with 1, so when you write: Jan(space)(space)Klaas or Jan(space)(space)(space)(space)Klaas, it would return Jan(space)Klaas.
I searched around for a while but somehow I just can't get this space matching to work..
PS: When I got this working, I will apply this for the mid and last name too ofcourse.
===========================================
EDIT: After you helping me out, I re-wrote my code to:
// validate usersurname
$usersurname = preg_replace("/\s{2,}/"," ", $usersurname);
if (!preg_match("/^[A-Za-z]+(\s[A-Za-z]+)*$/",$usersurname))
$usersurname_valid = 1;
// validate usermidname
$usermidname = preg_replace("/\s{2,}/"," ", $usermidname);
if (!preg_match("/^[A-Za-z]+(\s[A-Za-z]+)*$/",$usermidname))
$usermidname_valid = 1;
// validate userforename
$userforename = preg_replace("/\s{2,}/"," ", $userforename);
if (!preg_match("/^[A-Za-z]+(\s[A-Za-z]+)*$/",$userforename))
$userforename_valid = 1;
and the error notifications
elseif ($usersurname_valid !=1)
echo ("<p id='notification'>Only alphabetic character are allowed for the last name. $usersurname $usermidname $userforename</p>");
// usermidname character validation
elseif ($usermidname_valid !=1)
echo ("<p id='notification'>Only alphabetic character are allowed for the middle name. $usersurname $usermidname $userforename</p>");
// userforename character validation
elseif ($userforename_valid !=1)
echo ("<p id='notification'>Only alphabetic character are allowed for the (EDIT) first name. $usersurname $usermidname $userforename</p>");
Replacing the spaces are working well and I need this preg_match to check on on A-Za-z + space. I think in this case it doesn't matter if it's matching more than 1 spaces because it's replaced anyway, right?
EDIT:
Solution for my case:
$usersurname = preg_replace("/\s{2,}/"," ", $usersurname);
if (!preg_match("/[^A-Za-z ]/", $usersurname))
This does the work. Thanks for helping out, J0HN
Well, solving the problem you have in mind:
if (!preg_match("/^[A-Za-z]+(\s[A-Za-z]+)*$/",$usersurname)) { ... }
But, well, it's just a part of the solution, and it's not bulletproof. Look at the list of common mistakes when handling names.
So, you'd better to re-think on your validation approach.
Replacing the multiple spaces is simpler to achieve as a separate instruction, something like
$processed_usersurname = preg_replace("/\s{2,}/"," ", $usersurname);
This will match and replace any two or more consequent whitespace characters (space, tab, linebreak and carriage return) to single space
I would like to validate a string with a pattern that can only contain letters (including letters with accents). Here is the code I use and it always returns "nok".
I don't know what I am doing wrong, can you help? thanks
$string = 'é';
if(preg_match('/^[\p{L}]+$/i', $string)){
echo 'ok';
} else{
echo 'nok';
}
Add the UTF-8 modifier flag (u) to your expression:
/^\p{L}+$/ui
There is also no need to wrap \p{L} inside of a character class.
I don't know if this helps anybody that will check this question / thread later. The code below allows only letters, accents and spaces. No symbols or punctuation like .,?/>[-< etc.
<?php
$string = 'États unis and états unis';
if(preg_match('/^[a-zA-Z \p{L}]+$/ui', $string)){
echo 'ok';
} else{
echo 'nok';
}
?>
If you want to add numbers too, just add 0-9 immediately after Z like this a-zA-Z0-9
Then if you are applying this to form validation and you are scared a client/user might just hit spacebar and submit, just use:
if (trim($_POST['forminput']) == "") {... some error message ...}
to reject the submission.
I use a regular expression to on a user-inputted field to make sure that they have entered between 1 and 20 characters.
Here's the code:
$post_validations = array("title" => '/^[[:alnum:][:punct:][:space:]]{1,100}$/');
But whenever a user enters a foreign character, or a special quote character from MS Word (I can't paste it into here, it converts it to a normal quote!) the regex doesn't return true, and it shows an error.
I wondered what would be the best regex to use?
Thanks
If all you want is know that it is between 1 and 20 characters, why not use strlen() ?
$length = strlen($title);
if($length >= 1 and $length <=20)
echo "VALID";
else
echo "Invalid";
[EDIT]: Checking whether aplhanumeric or puctuation:
And if you also want to check whether the string contains any non-printable characters that may cause problem, just use ctype_graph()
if(ctype_graph ($title))
echo "Only alphanumeric or punctuation";
else
echo "Invalid non-printable characters found";
[EDIT 2]:
If you also want the spaces to be validated, just use this:
if(ctype_graph(str_replace(' ', '',$title))