PHP development · 2 min read · Updated

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);

Rupak Nepali
Written byRupak Nepali

Rupak Nepali is a web developer, manage Webocreation website and the author of six books:
Getting Started with OpenCart Module Development (Packt, 2013),
OpenCart Theme and Module Development (Packt, 2015),
OpenCart 4: Dev Guide for Themes & Extensions (2024),
OpenCart 4: User Manual (2025),
Cloudflare for the Enterprise Website (2026) and
The Technical Web Guide for Marketers (2026).
Rupak has built OpenCart stores and extensions since 2010, has published more than 600 tutorials on Webocreation, and shares free OpenCart extensions on GitHub.
Find Rupak on
GitHub | Amazon | LinkedIn | X | YouTube.

Leave a Reply

Your email address will not be published. Required fields are marked *