php preg_match() to validate input [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I would like to use the preg_match() to validate a textfield, that clients can later on insert it to the database.
However there is one thing that I don't understand. How would the preg_match() look if I want the client to enter eight numbers and then a dash (-) and four numbers. That input must match exactly
Therefore
N = number
Input:
NNNNNNNN-NNNN

The regex you want is this:
/^\d{8}-\d{4}$/
Demo
This says: match the start of the string (^), exactly 8 digits (\d{8}), a hyphen (-), exactly 4 digits (\d{4}), and then the end of the string ($).
So, the preg_match looks like this:
preg_match('/^\\d{8}-\\d{4}$/', $theData);

Related

Regex to validate the string with comma or space separated [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am trying to get string through input box from user
user can add only numbers in the string the format of a string is like this
Example
123456,111111,122335
123456 111111 122335
only "," or " " are allowed after 6 digits of number like show in example
You can use
^(?:(?:\d{6},)*|(?:\d{6}\s+)*)\d{6}$
Regex Demo
This regexp is for 3 groups of 6 digits:
\d{6}(?: |,)\d{6}(?: |,)\d{6}
You can test it here.

How to insert a dash at a specific position of a string in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a string for example 'AA231CS'and I want to add a hyphen or dash so it looks like this (the results should be this) 'AA-231-CS'.
and another string '9999ZZZ99' and I want to add a hyphen or dash so it looks like this (the results should be this) '9999-ZZZ-99'.
what's the best approach to handle this?
thank you!
Here is regex required and implode
$string = "9999ZZZ99AAA";
// I have separated string with continous numbers and alphabets in group
preg_match_all('/([0-9]+|[a-zA-Z]+)/',$string,$matches);
// imploding them by `-`
echo implode("-", $matches[0]);
Demo.

Check if the ninth number from the right id 5 using Regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
is it possible to check the ninth number from the right whether its 5 or not.
I want to check the 9th digit from the right is a five:
00966588696745
^-True
12088696748
^-False
is it possible ?
You can use a lookahead: 5(?=\d{8}$)
This will ensure that your test string contains a 5 followed by 8 digit and the end of the string.
A simple version of this would be as follows:
^5.{8}$
Explanation:
^ - start looking from beginning of the string
5 - you want to ensure that the string starts with 5
. - any character. But if you need to assure it is only digits you need to use \d instead (the \ is important)
{8} - you expect for 8 chars of any type (since we use dot: '.')
$ - means end of the string. This assures that you have exactly 8 chars before end of the string
I hope this answer is helpful and explains the parts inside the regexpr

Restricting input field characters to specific sequence of numbers and dashes in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I was wondering if there is a way for PHP to take a string and determine whether it follows a specific pattern of numbers and dashes. For instance, can it check to make sure that that the user inputs 00-00-000-000? So it would be two numbers, hyphen, two numbers, hyphen, three numbers, hyphen, three numbers.
Using anubhava's regex, here is the php:
if (preg_match("/^\d{2}-\d{2}-\d{3}-\d{3}$/", $string))
{
//the string is good
} else {
//the string is bad
die("Invalid string entered");
}
Sure you can match with this regex:
/^\d{2}-\d{2}-\d{3}-\d{3}$/

Match fixed string part 2 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have this fixed string below.
edbe801bf92fe7b770f72df2d722df0a
And i match what i need with this regex that someone helped me with and i sort of understand.
(?:[^7]*7){4}([^7]*)
Now i looked at character class use today but i dont know how to include a number to match a string like this except with 5 or 3 in same position as the string with 7
How can i also match these numbers and get my text between the last 5 and 3?
edbe801bf92fe5a250e57eb2d522ef0b
edbe801bf92fe3e532b3f0e2f3e0b5fe
All should be captured
2df2d
7eb2d
f0e2f
If you just want to skip the first twenty character and grab the next five, as your sample suggest, you can do it with this simple regular expresion.
^.{20}(.{5})
This regex give the desired output for all your test lines.

Categories