Splitting and Practical Patterns
Split strings with preg_split and build common validation patterns.
Splitting and Practical Patterns is a free PHP Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
preg_split() Signature
preg_split($pattern, $subject, $limit, $flags) splits a string by a regex delimiter and returns an array of substrings.
Simple Split
Split on any whitespace sequence.
<?php
$words = preg_split('/\s+/', "Hello World\tFoo");
print_r($words); // ["Hello", "World", "Foo"]Split with Limit
The third parameter limits how many pieces are returned. The last piece contains the remainder.
<?php
$parts = preg_split('/,\s*/', "a, b, c, d", 3);
print_r($parts); // ["a", "b", "c, d"]PREG_SPLIT_NO_EMPTY
The PREG_SPLIT_NO_EMPTY flag removes empty strings from the result — useful when the input has consecutive delimiters.
<?php
$parts = preg_split('/,/', "a,,b,,c", -1, PREG_SPLIT_NO_EMPTY);
print_r($parts); // ["a", "b", "c"]PREG_SPLIT_DELIM_CAPTURE
Include the delimiter (and its captured groups) in the result array.
<?php
$parts = preg_split('/(,)/', "a,b,c", -1, PREG_SPLIT_DELIM_CAPTURE);
print_r($parts); // ["a", ",", "b", ",", "c"]Email Validation Pattern
A practical RFC-5322-lite email regex:
<?php
function validEmail(string $s): bool {
return (bool) preg_match(
'/^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$/',
$s
);
}URL Validation Pattern
Validate http/https URLs.
<?php
function validUrl(string $s): bool {
return (bool) preg_match(
'#^https?://[\w\-]+(\.[\w\-]+)+(/[^\s]*)?$#i',
$s
);
}Phone Number Pattern
Allow optional country code, dashes, dots, or spaces between digit groups.
<?php
$pattern = '/^\+?[1-9]\d{0,2}[\s\-.]?\d{3}[\s\-.]?\d{4,7}$/';
preg_match($pattern, '+1 800 555-1234', $m);Password Strength Pattern
Require at least 8 chars with one uppercase, one digit, one special char.
<?php
function strongPassword(string $p): bool {
return preg_match('/^(?=.*[A-Z])(?=.*\d)(?=.*[^\w]).{8,}$/', $p);
}IPv4 Address Pattern
Validate an IPv4 address (each octet 0-255).
<?php
$ipv4 = '/^(25[0-5]|2[0-4]\d|[01]?\d\d?)(\.(25[0-5]|2[0-4]\d|[01]?\d\d?)){3}$/';Date Format Pattern
Validate YYYY-MM-DD date strings.
<?php
$date = '/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/';
var_dump((bool) preg_match($date, '2025-06-15')); // trueSummary
preg_split divides strings by a pattern. Practical patterns exist for emails, URLs, phones, and dates — always prefer filter_var for standard types.
Quick Check
Which flag removes empty strings from preg_split results?
Frequently asked questions
Is the “Splitting and Practical Patterns” lesson free?
Yes — the full text of “Splitting and Practical Patterns” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Splitting and Practical Patterns”?
Split strings with preg_split and build common validation patterns. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Splitting and Practical Patterns” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Regex Syntax Fundamentals
- Matching with preg_match
- Replacing Text with preg_replace
- Splitting and Practical Patterns