How can I get a string that only contains maximum of 11 or 16 characters of a regex pattern ?
if(!preg_match("/^[a-zA-Z0-9]+$/", $str) == 1) {
echo 'Invalid Characters';
}
echo 'Valid characters';
if Numeric, maximum length of 16 digits, If alpha-numeric, maximum length of 11 characters
Eg: Numeric = 0825551234
Eg: alpha-numeric = 1800House
Instead of outlining the rest of your requirements in comments you should update your question. Based on what I read, try this: /^[a-zA-Z0-9]{5,16}$/
Alpha-numeric, minimum 5 and maximum 16 characters repetition.
I am new to Regexp, but for me this is an answer for your question
if(preg_match("/^\d{1,16}$/", "12345678123456781") == 1)
{
echo "Numeric";
}
else if(preg_match("/^([\w\_]{1,11})$/", "qweqweqweqwe") == 1)
{
echo "Alphanumeric";
}
else
{
echo("some other stuff");
}
If you don't want to escape underscores, just replace second regexp with this /^(\\w{1,11})$/
if Numeric, maximum length of 16 digits, If alpha-numeric, maximum length of 11 characters
This should do it:
<?php
//$string="0123456789012345";/* 16 digits */
$string="012345abcde"; /* 11 alpha-numeric */
if( preg_match("/^[0-9]{1,16}$|^(?=.*[a-zA-Z])[a-zA-Z0-9]{1,11}$/", $string) )
{
echo 'valid Characters';
}
else
{
echo 'Invalid characters';
}
Quality Assurance: https://3v4l.org/1c2KD
Good luck!
if(ctype_digit($this->smsSenderId) && strlen($this->smsSenderId) > 16){
//'too long (maximum is 16 numeric characters)';
}
// alpha numeric can contain 11 characters
else if(strlen($this->smsSenderId) > 11){
//'too long (maximum is 11 alpha-numeric characters)';
}
else if(!preg_match("/^[0-9a-zA-Z]{4,11}$/", $this->smsSenderId) == 1){
//'Invalid characters. Eg: 0825551234 or 1800House';
}
You could do it without using a regex at all.
$strings = array('0825551234', '1800House', '111111111111111', 'aaaaaaaaaaa', '12!1adfas');
foreach($strings as $string){
echo "Testing for: $string";
if((is_numeric($string) && strlen($string) <=16) || (ctype_alnum($string) && strlen($string) <=11)) {
echo ' true' . "\n";
} else {
echo ' false' . "\n";
}
}
Output:
Testing for: 0825551234 true
Testing for: 1800House true
Testing for: 111111111111111 true
Testing for: aaaaaaaaaaa true
Testing for: 12!1adfas false
Or if you needed to regex:
$strings = array('0825551234', '1800House', '11122111111111111', 'aaaaaaaaaaa', '12!1adfas');
foreach($strings as $string){
echo "Testing for: $string";
if(preg_match('~^(\d{1,16}|[a-z0-9]{1,11})$~i', $string)) {
echo ' true' . "\n";
} else {
echo ' false' . "\n";
}
}
Regex101 demo: https://regex101.com/r/mN1aF8/1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.