How to Use preg_match for New Line - php

I prevent people from entering characters other than the one on preg_match with this code:
if (!preg_match("/^[a-zA-Z0-9 ]*$/",$input)) {
$error = "Error";
}
It only permits:
A to Z
0 to 9
(space)
But, if people click ENTER. data can not be sent. How do I have to change the code?

You can use \r\n for enter or new line in preg_match.
if (!preg_match("/^[a-zA-Z0-9 \r\n]*$/",$input)) {
$error = "Error";
}

try this
&[^a-zA-Z0-9\s]&
the \s should find any type of whitespace, including newlines and spaces. if you only want spaces and newlines (not tabs or other whitespace) you can do /^[a-zA-Z0-9 ]*$/ instead.

I think the above will work fine... but if it fails you can try this:
if (!preg_match("/(?!\n)^[a-zA-Z0-9\s]*$/",$input)) {
$error = "Error";
}
(?!\n) Negative look ahead will prevent \n from appearing in the string..

Related

PHP "preg_match" to check whether the text contains small characters [duplicate]

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.

how to look for space in preg_match

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.

Preg match and Preg replace specific format

I need help with preg match/replace forma i really cant understand how its working and what each element doing.
So far I have this:
$username = preg_replace('/\s+/', '_', $_POST['uname']);
if(preg_match('/^[a-zA-Z0-9]{5,12}+$/u', $username))
{
$username = trim(strip_tags(ucfirst($purifier->purify(#$_POST["uname"]))));
}
else
{
$message['uname']='wrong username input';
}
And for utf8(hebrew language) i got this:
if(preg_match("/^[\p{Hebrew} a-zA-Z0-9]{2,10}+$/u", $_POST['fname']))
{
//
}
which is working perfect, but I don't want to allow Hebrew on username just English.
I tried to play with that in multiple combinations, I tried to change but no success, and I did research on StackOverflow and Google but can't make it like I want I don't understand.
I used a RegEx site to and tried to build but with no success.
So until now I got this :
User can put 5-12 letters/numbers no special characters.
What i want is :
Can enter between 5-12 letters/numbers no special charcaters - i
already have it.
Allow whitespaces
preg_match if no mixed language's like E.G: $username = שדגדשsdsd; <- not allowed mixed languages.
And preg_replace to:
Replace white spaces to nothing (remove white spaces) i have this but i dont know if it correct:
$username = preg_replace('/\s+/', '', $_POST['uname']);
Also, I am using UTF-8 language .
EDIT:
With help of hwnd , i make it to work like i want the latest code:
if(preg_match('/^[\p{Hebrew}]{2,10}|[a-zA-Z]{2,10}$/u', $_POST['fname']) && preg_match('/^[a-zA-Z]{2,10}|[\p{Hebrew}]{2,10}$/u', $_POST['fname']))
{
$message = 'valid';
}else{
$message = 'Invalid';
}
Solved,Thanks.
I'm sure if your allowing whitespace in the username, you can suffice with just a space character but to be safe use \s which matches whitespace (\n, \r, \t, \f, and " "), for that you can just add that inside of your character class []
if (preg_match('/^[a-zA-Z0-9\s]{5,12}+$/u', $username)) { ...
And you can leave your preg_replace() function as is...
Update: To match different characters, but not mixed you could try the following:
$user = 'hwדגדשרביd'; // "invalid"
$user = 'fooo'; // "valid"
$user = 'שדגדשרביב'; // "valid"
if (preg_match('/^[\p{Hebrew}]{2,10}|[a-zA-Z]{2,10}$/u', $user)) {
echo "valid";
} else {
echo "invalid";
}
Your preg_replace for removing whitespace from the username is fine.
To allow only English letters, digits and whitespace in the username, use this:
if (preg_match('/^[a-zA-Z0-9\s]{5,12}+$/u', $username)) {
# $username is OK
}
else {
# $username is not OK
}

Check text box for spaces

I need help with code to output an error if a text box contains a space or hyphen. I have the following:
elseif($_REQUEST['students']['FIRST_NAME']!= "CONTAINS SPACE OR HYPHENS")
{
$insert_error = 'No spaces, hyphens, or spaces allowed in first name';
}
What function could I use? I have other functions that work for similar tasks such as:
elseif($_REQUEST['students']['PASSWORD']!=$_REQUEST['verify_password'])
{
$insert_error = 'Passwords did not match.';
}
I know it's quite straight forward, but I'm not sure what to use. Forgive me, I'm very rusty.
Use regular expressions and the preg_match() function.
if ( preg_match('/[\s-]/', $_REQUEST['students']['FIRST_NAME']) ) {
$insert_error = 'You cannot enter spaces or dashes';
}
The [\s-] inside of preg_match() is called a regular expression. The \s is for any whitespace character, and the - is for the dash.
More information here: http://php.net/manual/en/function.preg-match.php
$first_name = $_REQUEST['students']['FIRST_NAME'];
elseif((preg_match("/\\s/", $first_name) === true)
")
{
$insert_error = 'No spaces, hyphens, or spaces allowed in first name';
}

preg_match with international characters and accents

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.

Categories