Limit string length with RegEx - php

I wrote this regex for checking the input number from my form:
if (!preg_match("/^0\d{10}+|^9\d{9}+/",$_POST['number'])){
echo "Error";
}else{
echo "Ok";
}
this code will check the minimum length but if length is more than 10 or 9 characters, this regex cannot work !
What should I do ? should I check with strlen after Regex or I can limit the maximum length ?
UPDATE:
the string length should be exactly 10 characters if start with 0 and should be exactly 9 characters if start with 9, and should return false on another ways (more or less length, start with different numbers and ...)

Possibly you just want to anchor the string at the end using $. Also can drop the + after the numeric quantifier:
^(?:0\d{9}|9\d{8})$
See test at regex101.com

You can use this regex:
'/^(0\d{9}|9\d{8})$/'
This will allow string length exactly 10 characters if it starts with 0 and should be exactly 9 characters if input starts with 9.

Related

Set maximum length in regex

I have been googling this looking for a similar example. I need to use the following regex (which works) but limit the number of allowed characters to 20:
/^[A-Za-z0-9]+(?:[#_\-.][A-Za-z0-9]+)*$/
the problem is that the only examples are using a far simpler match pattern which I cannot seem to emulate in the above:
e.g /^.{1,35}$/
I think the character length which you want to allow would be from 1 to 20. If yes then you could try the below regex.
^(?=.{1,20}$)[A-Za-z0-9]+(?:[#_\-.][A-Za-z0-9]+)*
DEMO
(?=.{1,20}$) positive lookahead at the first asserts that the character length must be from 1 to 20.
Use strlen() to verify the length:
if (strlen($value) > 20)
{
// Too long
}
else
{
// OK, perform regex
}
#wavemode Limit the first match to one character, and then make sure the length of the second match is between 0 and 19:
Add alphanum repetitions between 2 and first symbol.
/^[A-Za-z0-9](?:[A-Za-z0-9]*[#_\-.][A-Za-z0-9]+){0,19}$/

Check POST with preg_match [duplicate]

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)
{
//...
}

check if 5 digit number is available in a string

$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.

PHP / regular expression to check if a string contains a word of a certain length

I need to check whether a received string contains any words that are more than 20 characters in length. For example the input string :
hi there asssssssssssssssssssskkkkkkkk how are you doing ?
would return true.
could somebody please help me out with a regexp to check for this. i'm using php.
thanks in advance.
/\w{20}/
...filller for 15 characters....
You can test if the string contains a match of the following pattern:
[A-Za-z]{20}
The construct [A-Za-z] creates a character class that matches ASCII uppercase and lowercase letters. The {20} is a finite repetition syntax. It's enough to check if there's a match that contains 20 letters, because if there's a word that contains more, it contains at least 20.
References
regular-expressions.info/Character Classes and Finite Repetition
PHP snippet
Here's an example usage:
$strings = array(
"hey what the (##$&*!#^#*&^#!#*^##*##*&^#!*#!",
"now this one is just waaaaaaaaaaaaaaaaaaay too long",
"12345678901234567890123 that's not a word, is it???",
"LOLOLOLOLOLOLOLOLOLOLOL that's just unacceptable!",
"one-two-three-four-five-six-seven-eight-nine-ten",
"goaaaa...............aaaaaaaaaalll!!!!!!!!!!!!!!",
"there is absolutely nothing here"
);
foreach ($strings as $str) {
echo $str."\n".preg_match('/[a-zA-Z]{20}/', $str)."\n";
}
This prints (as seen on ideone.com):
hey what the (##$&*!#^#*&^#!#*^##*##*&^#!*#!
0
now this one is just waaaaaaaaaaaaaaaaaaay too long
1
12345678901234567890123 that's not a word, is it???
0
LOLOLOLOLOLOLOLOLOLOLOL that's just unacceptable!
1
one-two-three-four-five-six-seven-eight-nine-ten
0
goaaaa...............aaaaaaaaaalll!!!!!!!!!!!!!!
0
there is absolutely nothing here
0
As specified in the pattern, preg_match is true when there's a "word" (as defined by a sequence of letters) that is at least 20 characters long.
If this definition of a "word" is not adequate, then simply change the pattern to, e.g. \S{20}. That is, any seqeuence of 20 non-whitespace characters; now all but the last string is a match (as seen on ideone.com).
I think the strlen function is what you looking for. you can do something like this:
if (strlen($input) > 20) {
echo "input is more than 20 characters";
}

How can I randomize an entire string of 62 characters?

I have 62 base64 characters that I want to randomize. How can I do this using PHP? The string would be all letters, upper and lower case as well as numbers from 0-9.
The thing that is most important to me is that the entire string be evaluated before a return value is given. In other words, if I request a string of 8 characters in length and my string starts out like:
1234567890ABCDE..... I don't want to get the first 8 numbers randomized. It should randomize the entire string first, then return 8 characters from that.
Try this:
$string = '1234567890ABCDE...';
$string = substr(str_shuffle($string), 0, 8);
str_shuffle randomizes the string, then substr takes the first 8 characters from it.
Take a look at str_shuffle.

Categories