0
0
PHPprogramming~10 mins

Preg_split for splitting in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Preg_split for splitting
Input string
Define regex pattern
Call preg_split with pattern and string
Regex matches delimiters
Split string at matches
Return array of substrings
The preg_split function takes a string and a regex pattern, finds matches for the pattern, and splits the string at those matches, returning an array of parts.
Execution Sample
PHP
<?php
$input = "apple,banana;cherry orange";
$pattern = "/[ ,;]+/";
$result = preg_split($pattern, $input);
print_r($result);
?>
This code splits the input string by commas, semicolons, or spaces using preg_split and prints the resulting array.
Execution Table
StepActionRegex MatchResulting Array
1Start with input stringN/A["apple,banana;cherry orange"]
2Define regex pattern /[ ,;]+/N/A["apple,banana;cherry orange"]
3Call preg_split with pattern and inputMatches commas, semicolons, spaces["apple", "banana", "cherry", "orange"]
4Print resulting arrayN/A["apple", "banana", "cherry", "orange"]
💡 All delimiters matched and string split into 4 parts
Variable Tracker
VariableStartAfter preg_splitFinal
$input"apple,banana;cherry orange""apple,banana;cherry orange""apple,banana;cherry orange"
$patternundefined"/[ ,;]+/""/[ ,;]+/"
$resultundefined["apple", "banana", "cherry", "orange"]["apple", "banana", "cherry", "orange"]
Key Moments - 2 Insights
Why does preg_split split at commas, semicolons, and spaces all at once?
Because the regex pattern /[ ,;]+/ matches one or more of any of these characters, so preg_split uses all of them as delimiters (see execution_table step 3).
What happens if the input string starts or ends with a delimiter?
preg_split will create empty strings at the start or end of the array unless you use the PREG_SPLIT_NO_EMPTY flag (not shown here). This is why trimming delimiters matters.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what does preg_split return?
A["apple,banana;cherry orange"]
B["apple", "banana", "cherry", "orange"]
C["apple", "banana;cherry orange"]
D["apple banana cherry orange"]
💡 Hint
Check the 'Resulting Array' column at step 3 in the execution_table.
At which step does preg_split actually split the string?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for when the array changes from one string to multiple parts in the execution_table.
If the pattern was changed to "/[,]+/", what would be the result array?
A["apple", "banana;cherry orange"]
B["apple", "banana", "cherry", "orange"]
C["apple,banana;cherry orange"]
D["apple banana cherry orange"]
💡 Hint
Changing the pattern to only commas means only commas split the string, so semicolons and spaces remain inside parts.
Concept Snapshot
preg_split(pattern, string) splits a string by regex matches.
Pattern defines delimiters.
Returns array of substrings.
Multiple delimiters can be combined in pattern.
Use flags to control empty parts.
Full Transcript
This visual trace shows how preg_split works in PHP. We start with an input string containing words separated by commas, semicolons, and spaces. We define a regex pattern that matches any of these delimiters. When preg_split is called, it finds all matches of the pattern and splits the string at those points, returning an array of the separated words. The execution table shows each step, including the initial string, pattern definition, splitting action, and final output. The variable tracker follows the values of input, pattern, and result variables. Key moments clarify why multiple delimiters work together and what happens with delimiters at string edges. The quiz tests understanding of the splitting step, output array, and effect of changing the pattern. The snapshot summarizes the main points about preg_split usage.