Php preg_match error message - php

I want to match a string using REGEX which follows the syntax:
Text/number
So my preg_match() function is...
if(preg_match("/[^A-Za-z/0-9]$/ ", $folio))
$err[] = "Wrong value, it's should be lik: C/455";
But getting error message...

Try this:
$folio = "Text/15";
if(preg_match('~[a-z]/[\d]~i', $folio))
echo "match";
else
echo "no match";

You needed to escape / using \. Also numbers is a subset of text and you need to include it in your text part. You need one or more text/numeric characters, so a + is required.
It adds up to the following statement:
if(preg_match("/^[A-Za-z0-9]+\/[0-9]+$/", $folio))

You either need to escape / or use different char to surround regexp, for example:
if(preg_match("#[^A-Za-z/0-9]$# ", $folio))

if ( ! preg_match('/\b[a-z]+\/[0-9]+\b/i', $folio)) {
$err[] = "Wrong value, it's should be like this: C/455";
}

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 check if string contains square brackets php

I have a strange question maybe you could help me with. I am trying to check whether the given string contains special characters. The code below is working however one character seems to get exempted on the condition which is the square brackets [ ]. Can you help me with this? thank you.
$string = 'starw]ars';
if (preg_match('/[\'^£$%&*()}{##~?><>,|=_+¬-]/', $string)) {
echo 'Output: Contains Special Characters';
}else{
echo 'Output: valid characters';
}
Please note: I can't use below condition since I need to accept others characters from other languages like in arabic, chinese, etc. so it means I need to specify all characters that is not allowed.
if (!preg_match('/[^A-Za-z0-9]/', $string))
Appreciate your help. Thanks.
You forgot to add square brackets [] in your expression. I have added this \[\] in your current expression.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$string = 'starw]ars';
if (preg_match('/[\[\]\'^£$%&*()}{##~?><>,|=_+¬-]/', $string))
{
echo 'Output: Contains Special Characters';
} else
{
echo 'Output: valid characters';
}
Use strpos:
$string = 'starw]ars';
if (strpos($string, ']') !== false) {
echo 'true';
}
Please see the following answer for additional information:
How do I check if a string contains a specific word in PHP?
You should add escaped square brackets to your expression.
preg_match('/[\'^£$%&*()}{##~?><>,|=_+¬-\[\]]/', $string)
EDIT: Apologies to #Thamilan, I didn't see your comment.
EDIT 2: You could also use the preg_quote function.
preg_match(preg_quote('\'^£$%&*()}{##~?><>,|=_+¬-[]', '/'), $string);
The preg_quote function will escape your special characters for you.
Try this example:
<?php
$string = 'starw]]$%ars';
if (preg_match('/[\'\/~`\!##\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/', $string))
{
echo 'Output: Contains Special Characters';
} else
{
echo 'Output: valid characters';
}
?>
Output:
Output: Contains Special Characters

How to user pregmatch in php

I am trying to find if a persons name contains any numbers or symbols. I am not very sure how to use preg_match and all the examples online make no sense can someone please explain how i can check if a value has numbers or symbols. And if you can please explain how it works.
To check if the person name contains only number. The code below has been tested and is working
<?php
error_reporting(0);
$number = '001222288';
if (!preg_match('/^[0-9]*$/', $number)) {
//error
echo 'Error does not contain only number';
} else {
echo 'success. it contains only number';
}
?>
if the variable number contains any other thing that is not number it result in error. eg
$number = 'ABFRT001222288';
To check for alphabets or string
<?php
error_reporting(0);
$string = 'ABTYUUU';
if (!preg_match('/^[A-Z]*$/', $string)) {
//error
echo 'Error does not contain only alphabet';
} else {
echo 'success. it contains only alphabets';
}
?>
Mark this as correct answer if it solve your problem
Thanks
The function preg_match use regex expressions. You can use an online tool for test your regex. I use debuggex, preg_match use PCRE expressions. In your case you should, you may use for numbers
preg_match("/[0-9]+/", $subject);
EDIT : We need more details for the symbols you want select

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.

Find if numeric character in PHP String

Sorry for asking this simple question, but I seem not to find any answers.
How can you check whether there is a number within a string?
I've tried:
$string = "this is a simple string with a number 2432344";
if (preg_match('/[^0-9]/', "$string")) {
echo "yes a number";
} else {
echo "no number";
}
Doesn't seem to work...
If you want to find a number, don't negate the character set with ^ in your regular expression. That means "match anything except a number".
$string = "this is a simple string with a number 2432344";
if (preg_match('/[0-9]/', "$string")) {
echo "yes a number";
} else {
echo "no number";
}
Also, you could just use \d instead of [0-9], and $string instead of "$string".
^ will only negate what's in the regex. You don't need to use it.

Categories