This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regular expression to limit number of characters to 10
I retrieve a POST to a standard form and I need to test two things:
The value must be 40 characters
The value must contain only letters and numbers
I think it is possible to do this with preg_match, but I do not know how.
In the global $_POST you have all your posted data on the http request.
then:
$myvar = $_POST['your_posted_variable_here'];
$result = preg_match('/^([\w\d]){40}$/i', $myvar);
$result will be true if your posted data only contains letters and digits and is 40 characters long, otherwise will be false.
For exactly 40 characters:
^[a-zA-Z0-9]{40}$
For at least 40 characters:
^[a-zA-Z0-9]{40,}$
Information on preg_match
http://php.net/manual/en/function.preg-match.php
if (preg_match("#^[a-z0-9]{40}$#mis", $_POST['username'])) {
print 'matched';
}
You can also check the length of the string using strlen() first, then if it satisfies the desired length, go forth with the checking of alpha numerics.
But General has the right idea...
Here's another way:
preg_match("/^[0-9a-zA-Z_]{40,}$/", $_POST["something"])
This is alpha numeric, and checks for at least 40 characters, but will accept more. The missing value after the comma means that it can be of any value equal or bigger than 40.
PHP provides a function for checking alphanumeric characters ctype_alnum and strlen to check the length of a string so using both functions you can validate it
$yourInput=$_POST['yourInput'];
if(ctype_alnum($yourInput) && strlen($strlen)==40)
{
//...
}
Related
This question already has answers here:
substr() return incorrect number of characters
(2 answers)
Closed 3 years ago.
I am trying to use substr to get part of string with index
<?php
echo substr("2019-10-10T02:10:30.413291+05:30",11,16);
?>
but the output is 02:10:30.413291+
what is know the output should have been 02:10
I can't get what am I doing wrong please help
Read the docs. The third parameter is length - how many chars you want to "select" past start.
If length is given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of string).
echo substr("2019-10-10T02:10:30.413291+05:30", 11, 5);
This outputs 02:10.
The third parameter in substr() is the length of the substring you wish to grab (not the index of the end of the substring):
If length is given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of string).
In order to grab your desired substring of 2:10, you're looking for a length of 5:
echo substr("2019-10-10T02:10:30.413291+05:30",11,5); // 2:10
This can be seen working here.
This question already has answers here:
PHP Regex to Find Any Number at the Beginning of String
(2 answers)
Closed 3 years ago.
I have strings with the following pattern: 12345ABCDE6789 where each group of numbers and letters are a variable length. There will only ever be numbers and letters, no special characters.
I need a way to get the numbers up until the first letter, so that the example above would return 12345.
My thought was I could find the string position of the first letter and then trim the string to that position. But I'm having trouble figuring out how to get that index without knowing what the character is. Other solutions I've found have had the knowledge that the first letter would be an "A" where mine could be any letter.
Is there a concise way to do this?
I do not have much experience with regex, but maybe something there would be a better solution to this problem?
<?php
$re = '/^[0-9]+/m';
$str = '12345ABCDE6789';
preg_match($re, $str, $matches);
var_dump($matches[0]); // 12345
https://regexr.com/4mcfr
So long as the number will be <= 2147483647 for 32 bit systems and <= 9223372036854775807 for 64 bit, then the simplest is to cast to an integer and it will truncate letters (anything that will not return a valid integer):
echo (int)"12345ABCDE6789";
Returns 12345
$st1='dsdsdsd 97537 sdsdd dsddd';
$st2='fdsf 23e sdsd 434 432443454';
$st3='fdf97537 ds344dsddd';
I want to check whether a 5 digit number is available in a string.
st1-- has 5 digit number
st2--- not
st3-- has 5 digit number
A simple regex will do the job.
preg_match('/\d{5}/', $input)
See also http://www.php.net/preg_match
Try this regular expression with preg_match() or preg_match_all()
preg_match("/\b[^\d]*\d{5}[^\d]*\b/", $str);
Let's assume that each element to be checked if this is five digits number is separated by space in string. Therefore you may use explode function to convert string into array of substrings. next you can use is_numeric function to check if that is digit along with check if that sub string is five length long. Also you may use regular expression for that.
Here RegEx is far more better. As I see the #Matt's answer meets these requirements, my comments will be unnecessary.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What's the correct way to test if a variable is a number in PHP?
I have two variables coming in from a form:
$minNo = mysql_real_escape_string($_GET["minNo"]);
$maxNo = mysql_real_escape_string($_GET["maxNo"]);
How can I check whether they are numerical values or not?
Try is_numeric.
Finds whether the given variable is numeric. Numeric strings consist
of optional sign, any number of digits, optional decimal part and
optional exponential part. Thus +0123.45e6 is a valid numeric value.
Hexadecimal notation (0xFF) is allowed too but only without sign,
decimal and exponential part.
Use is_numeric function, it returns bool value:
is_numeric($element)
You can use is_numeric() to check if the number is numeric.
To validate as an integer, use...
$validInt = filter_var($minNo, FILTER_VALIDATE_INT);
using is_numeric() function...
This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 4 years ago.
I've been searching for hours to find a regex that does this for me, all the ones I've found either require dashes or limit something else that I don't need.
Like this one:
^(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$
Basically I want to allow these types of input for phone number, these are all valid:
+000 111111111
+00011111111111
0022 111111111111
0022111111111
+333-4444-5555-6666
000-7878787-0000-4587
Note that the number of digits is not limited, I only want the validation to filter out empty inputs and the alphabet. Also filter out all other characters except a maximum of 4 dashes, max. 4 spaces and an optional single plus sign.
Is this possible through preg_match or not?
Any help is appreciated, thanks!
Sure its possible. But to my opinion dangerous to use stuff that is not understood. I would do something like this
^(?!(?:\d*-){5,})(?!(?:\d* ){5,})\+?[\d- ]+$
See it here on Regexr
The last part \+?[\d- ]+ allows an optional + followed by at least one digit, dash or space
The negative lookaheads ensure that there are not more than 4 dash or spaces.
Limitations:
- The dash or space can be in one row
- it accepts also - as valid
Try it yourself on the Regexr link, you can just add examples what you want.
Strip wanted characters out (" ", "-"), count the amount then chuck an if statement if count <= 4 (for the "+" character it would be == 1). So in total it would be
if (countSpace <= 4 && countDash <= 4 && countPlus == 1) {
...
}
As for being empty, just use the standard form validation for checking if the input has been filled or not.