How to add and OR statement correctly - php

I am trying to do a comparison of serial numbers like so 20140831-123 or 20140831-1234 so the form can accept our new serial numbers which contain 4 last numbers. So far I have tried an elseif statement and an or operator with no results what am I doing wrong? is there a way to change the reg expression itself to accept 3 or 4 digits at the end of the serial?
if($name == 'newserial1'){
$newserial1 = $_POST['newserial1'];
if($newserial1 != '') {
if(!preg_match('/^([0-9]{8}-)([0-9]{3})$/', $newserial1) ||
(!preg_match('/^([0-9]{8}-)([0-9]{4})$/', $newserial1))) {
$result['valid'] = false;
$result['reason'][$name] = 'Incorrect Serial Number.';
}
}
}

Just use the below regex to match last 3 or 4 digits also,
^([0-9]{8}-)([0-9]{3,4})$
DEMO
Explanation:
^ Asserts that we are at the start.
([0-9]{8}-) Captures 8 digits and a following - symbol.
([0-9]{3,4}) Remaining three or four digits are captured by the second group.
$ Asserts that we are at the end.

Use \d{3,4}$ to match 3 or 4 digit in the end
Here is the complete regex pattern
^(\d{8})-(\d{3,4})$
Here is online demo
Pattern explanation:
^ the beginning of the string
( group and capture to \1:
\d{8} digits (0-9) (8 times)
) end of \1
- '-'
( group and capture to \2:
\d{3,4} digits (0-9) (between 3 and 4 times)
) end of \2
$ the end of the string

Your code works fine, just remove Not operator from your if clauses, and add matches to preg_match:
if($name == 'newserial1'){
$newserial1 = $_POST['newserial1'];
if($newserial1 != '') {
if(preg_match('/^([0-9]{8}-)([0-9]{3})$/', $newserial1, $matches) ||
(preg_match('/^([0-9]{8}-)([0-9]{4})$/', $newserial1, $matches))) {
//$result['valid'] = false;
//$result['reason'][$name] = 'Incorrect Serial Number.';
$result['matches'] = $matches[2];
}
}
}

Related

Regular expression to validate employee id

I want a regular expression to validate employee ids which are
UC-00001,
UC-00012,
UC-000100. etc
"UC-000" is constant but after that if one digit number exist it becomes UC-00001 and if more den one digit exist then only three zeros needs to be constant.(UC-00010)
I tried using preg_match(/[U]{1}[C]{1}-[0]{3}[0-9]$/) but its not validating properly.
Thanks in advance
You can match three zeroes and then either from 00 to 09 or from 10 up..
^UC-000(?:0\d|[1-9]\d*)$
The pattern matches:
^ Start of string
UC-000 Match literally
(?: Non capture group
0\d Match 0 and a single digit 0-9 (Or use [1-9] to not match 00000)
| Or
[1-9]\d* Match a digit 1-9 and optional digits
) Close non capture group
$ End of string
Regex demo
$strings = [
"UC-00001",
"UC-00012",
"UC-000100",
"UC-00001",
"UC-00010",
"UC-00000",
"UC-000010",
"UC-0000100"
];
$pattern = "~^UC-000(?:0\d|[1-9]\d*)$~";
foreach ($strings as $s) {
if (preg_match($pattern, $s)) {
echo "Match: $s" . PHP_EOL;
} else {
echo "Not match: $s" . PHP_EOL;
}
}
Output
Match: UC-00001
Match: UC-00012
Match: UC-000100
Match: UC-00001
Match: UC-00010
Match: UC-00000
Not match: UC-000010
Not match: UC-0000100
Your pattern is logically correct, save that you didn't allow for multiple digits after the leading 000. I would use this version:
^UC-[0-9]{5,}$
PHP script:
$input = "UC-000100";
if (preg_match("/^UC-[0-9]{5,}$/", $input)) {
echo "VALID";
}

How can I search for duplicate numbers next to each other?

I'm trying to use preg_match() in PHP to make a pattern match for 4 digit numbers that have this pattern:
0033
1155
2277
Basically the first 2 digits are the same and the last 2 digits are the same, but not all 4 digits are the same.
Is preg_match() even the correct function to use? Or should I just split them up and match that way?
To search this kind of number in a text you can use:
preg_match('~\b(\d)\1(?!\1)(\d)\2\b~', $str, $m)
(or with preg_match_all if you want all of them)
details:
~ # pattern delimiter
\b # word boundary (to be sure that 1122 isn't a part of 901122)
(\d) # capture the first digit in group 1
\1 # back-reference to the group 1
(?!\1) # negative lookahead: check if the reference doesn't follow
(\d) #
\2 #
\b # word boundary (to be sure that 1122 isn't a part of 112234)
~ #
If you want to check if an entire string is the number, use the string limit anchors in place of word boundaries:
~\A(\d)\1(?!\1)(\d)\2\z~
You could use something like array_filter with a comparison callback:
function compare($number) {
// Compare first 2 numbers
if (intval($number[0]) !== intval($number[1])) {
return false;
}
// Compare last 2 numbers
if (intval($number[2]) !== intval($number[3])) {
return false;
}
// Make sure first and last numbers aren't the same
if (intval($number[0]) === intval($number[3])) {
return false;
}
return true;
}
$data = array_filter($data, 'compare');
You could also do this using a closure:
$data = array_filter($data, function($number) {
return intval($number[0]) === intval($number[1]) && intval($number[2]) === intval($number[3]) && intval($number[0]) !== intval($number[3]);
});
Examples here: http://ideone.com/0VwJz8

Regular expression doesn't work

I need to match following pattern using php Regular expression but it doesn't give expected out come.
ex:-
need to match pattern
5555 5545 9930
$id = "4567 3423 4567";
$regex = "/^[1-9]\d{4} \d{4} \d{4}$/";
if (preg_match($regex, $id)) {
// Indeed, the expression "^[2-9]\d{2} \d{3} \d{4}$" matches the date string
echo "Found a match!";
}else {
// If preg_match() returns false, then the regex does not
// match the string
echo "The regex pattern does not match. :(";
}
If you want to match: 4 non-zero digits + space + 4 digits + space + 4 digits
^([1-9]){4} \d{4} \d{4}$ should do the trick
I think the safest way to modify the existing regex is by adding an alternative to the first [2-9]\d{2} \d{3}:
^(?:[2-9]\d{2} \d{3}|\d{4} \d{4}) \d{4}$
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
See the regex demo
Details:
^ - start of string
(?:[2-9]\d{2} \d{3}|\d{4} \d{4}) - one of the alternatives:
[2-9]\d{2} \d{3} - a digit from 2 to 9, any 2 digits, a space and 3 digits
| - or
\d{4} \d{4} - 4 digits, space, 4 digits (for the new string types)
- a space
\d{4} - 4 digits
$ - end of string.
See the PHP demo:
$id = "4567 3423 4567";
$regex = "/^(?:[2-9]\d{2} \d{3}|\d{4} \d{4}) \d{4}$/";
if (preg_match($regex, $id)) {
echo "Found a match!";
} else {
echo "The regex pattern does not match. :(";
}
Simple edit of your regular expression will be
^[1-9]\d{3} \d{4} \d{4}

php regular expression minimum and maximum length doesn't work as expected

I want to create a regular expression in PHP, which will allow to user to enter a phone number in either of the formats below.
345-234 898
345 234-898
235-123-456
548 812 346
The minimum length of number should be 7 and maximum length should be 12.
The problem is that, the regular expression doesn't care about the minimum and maximum length. I don't know what is the problem in it. Please help me to solve it. Here is the regular expression.
if (preg_match("/^([0-9]+((\s?|-?)[0-9]+)*){7,12}$/", $string)) {
echo "ok";
} else {
echo "not ok";
}
Thanks for reading my question. I will wait for responses.
You should use the start (^) and the end ($) sign on your pattern
$subject = "123456789";
$pattern = '/^[0-9]{7,9}$/i';
if(preg_match($pattern, $subject)){
echo 'matched';
}else{
echo 'not matched';
}
You can use preg_replace to strip out non-digit symbols and check length of resulting string.
$onlyDigits = preg_replace('/\\D/', '', $string);
$length = strlen($onlyDigits);
if ($length < 7 OR $length > 12)
echo "not ok";
else
echo "ok";
Simply do this:
if (preg_match("/^\d{3}[ -]\d{3}[ -]\d{3}$/", $string)) {
Here \d means any digits from 0-9. Also [ -] means either a space or a hyphen
You can check the length with a lookahead assertion (?=...) at the begining of the pattern:
/^(?=.{7,12}$)[0-9]+(?:[\s-]?[0-9]+)*$/
Breaking down your original regex, it can read like the following:
^ # start of input
(
[0-9]+ # any number, 1 or more times
(
(\s?|-?) # a space, or a dash.. maybe
[0-9]+ # any number, 1 or more times
)* # repeat group 0 or more times
)
{7,12} # repeat full group 7 to 12 times
$ # end of input
So, basically, you're allowing "any number, 1 or more times" followed by a group of "any number 1 or more times, 0 or more times" repeat "7 to 12 times" - which kind of kills your length check.
You could take a more restricted approach and write out each individual number block:
(
\d{3} # any 3 numbers
(?:[ ]+|-)? # any (optional) spaces or a hyphen
\d{3} # any 3 numbers
(?:[ ]+|-)? # any (optional) spaces or a hyphen
\d{3} # any 3 numbers
)
Simplified:
if (preg_match('/^(\d{3}(?:[ ]+|-)?\d{3}(?:[ ]+|-)?\d{3})$/', $string)) {
If you want to restrict the separators to be only a single space or a hyphen, you can update the regex to use [ -] instead of (?:[ ]+|-); if you want this to be "optional" (i.e. there can be no separator between number groups), add in a ? to the end of each.
if (preg_match('/^(\d{3}[ -]\d{3}[ -]\d{3})$/', $string)) {
may it help you out.
Validator::extend('price', function ($attribute, $value, $args) {
return preg_match('/^\d{0,8}(\.\d{1,2})?$/', $value);
});

php if preg_match 4 numbers then a dot and then 2 numbers

I am trying to write an if statement in php with preg_match to say allow 4 numbers and then a dot and then 2 numbers...
This is what I have....
$string = "10000.000";
if (preg_match('[/^\d{0,4}(\.\d{1,2})?$/]', $string)){
return TRUE;
} else {
return FALSE;
}
is my preg_match code wrong?
This should work as you want it!
^[0-9]{4}\.[0-9]{2}$
^ start of string,
[0-9] a number from 0 to 9,
{4} 4x times,
\. a dot,
$ end of the string

Categories