Home Drupal Regex to find the four sequential characters in PHP

Regex to find the four sequential characters in PHP

Need to check if a string contains four sequential characters or consecutive characters/strings or identical letters/numbers/string/characters in PHP then following regex and code can be useful for you. I tested it with following code and regex is generated from

<?php
$re = '/(abcd|bcde|cdef|defg|efgh|fghi|ghij|hijk|ijkl|jklm|klmn|lmno|mnop|nopq|opqr|pqrs|qrst|rstu|stuv|tuvw|uvwx|vwxy|wxyz)+/xi';
$str = 'rupakabcd test';
if(preg_match_all($re, $str, $matches)){
  echo "Contains sequential characters";
}else{
  echo "Does not contain sequential characters";
}

If you wanted to check case sensitive then remove ‘i’ from $re variable at the end.

Comment if you find any easy way to make it works.

Regex to find identical characters:

(\w)(\1+){3}

$re = '/(\w)(\1+){3}/i';
$str = 'aaaa
rrrr
rupak
rupakaaaaarrr';

preg_match_all($re, $str, $matches);

// Print the entire match result
print_r($matches);

Regex to find identical numbers:
(\d)\1{3,}

$re = '/(\d)\1{3,}/';
$str = '123452222222222222
1234
1111
12345
654128
45721111572000004520
aaaa
rupak
';

preg_match_all($re, $str, $matches);

// Print the entire match result
print_r($matches);

Regex to find five sequential numbers:

(?: 0(?=1|\b)| 1(?=2|\b)| 2(?=3|\b)|  3(?=4|\b)|  4(?=5|\b)|  5(?=6|\b)|  6(?=7|\b)|  7(?=8|\b)|  8(?=9|\b)|  9\b  ){5}

$re = '/(?: 0(?=1|\b)| 1(?=2|\b)| 2(?=3|\b)|  3(?=4|\b)|  4(?=5|\b)|  5(?=6|\b)|  
6(?=7|\b)|  7(?=8|\b)|  8(?=9|\b)|  9\b  ){5}/x';
$str = '123 321 56789 890123 321098 134 333 421 12345 6789 
15246
1234567890
67890
12345
23456
34567
456789
56789
';

preg_match_all($re, $str, $matches);

// Print the entire match result
print_r($matches);

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here