Preg_split for splitting in PHP - Time & Space Complexity
We want to understand how the time it takes to split a string using preg_split changes as the string gets longer.
How does the work grow when the input string grows?
Analyze the time complexity of the following code snippet.
$string = "apple,banana,orange,grape";
$pattern = "/,/";
$result = preg_split($pattern, $string);
foreach ($result as $fruit) {
echo $fruit . "\n";
}
This code splits a string by commas into an array of fruits, then prints each fruit.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The preg_split function scans the entire string to find all commas and split accordingly.
- How many times: It processes each character in the string once to check for the pattern.
As the string gets longer, preg_split checks more characters to find split points.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows roughly in direct proportion to the string length.
Time Complexity: O(n)
This means the time to split grows linearly with the length of the input string.
[X] Wrong: "preg_split runs instantly no matter how long the string is."
[OK] Correct: The function must check each character to find split points, so longer strings take more time.
Understanding how string operations scale helps you write efficient code and explain your reasoning clearly in interviews.
"What if we changed the pattern to split by multiple characters or a more complex regex? How would the time complexity change?"
Practice
preg_split do?Solution
Step 1: Understand the function purpose
preg_splitis designed to split strings based on patterns, not fixed characters.Step 2: Compare options with function behavior
Only Splits a string into parts using a regular expression pattern. describes splitting using a regular expression pattern, which matchespreg_splitfunctionality.Final Answer:
Splits a string into parts using a regular expression pattern. -> Option DQuick Check:
preg_split splits string by regex pattern = A [OK]
- Confusing preg_split with implode or join functions
- Thinking preg_split replaces text instead of splitting
- Assuming preg_split counts words
preg_split?Solution
Step 1: Identify correct parameter order
The first parameter ofpreg_splitis the regex pattern as a string, the second is the input string.Step 2: Check regex pattern format
The pattern must be enclosed in delimiters like slashes/, /. preg_split('/,/', $string); uses this correctly.Final Answer:
preg_split('/,/', $string); -> Option AQuick Check:
Pattern first with slashes, then string = B [OK]
- Omitting regex delimiters like slashes
- Swapping the order of parameters
- Using plain string instead of regex pattern
$text = "apple,banana;orange|grape";
$result = preg_split('/[,;|]/', $text);
print_r($result);Solution
Step 1: Understand the regex pattern
The pattern/[,;|]/matches any comma, semicolon, or pipe character as separators.Step 2: Split the string by these separators
The string splits at each comma, semicolon, and pipe, resulting in four parts: "apple", "banana", "orange", "grape".Final Answer:
["apple", "banana", "orange", "grape"] -> Option CQuick Check:
Split by , ; | = ["apple", "banana", "orange", "grape"] [OK]
- Not recognizing all separators in regex
- Expecting only comma to split
- Confusing output format with string instead of array
$str = "one two three";
$parts = preg_split(' ', $str);
print_r($parts);Solution
Step 1: Check the pattern parameter
The pattern passed is a plain space character without regex delimiters, which is invalid syntax forpreg_split.Step 2: Correct pattern format
Regex patterns must be enclosed in delimiters like slashes, e.g.,'/ /'to match a space.Final Answer:
The pattern is missing regex delimiters. -> Option BQuick Check:
Regex pattern needs delimiters like slashes [OK]
- Using plain string without delimiters as pattern
- Assuming preg_split works like explode for spaces
- Blaming print_r for output issues
preg_split call achieves this?Solution
Step 1: Understand the pattern and flags
The pattern/[ ,]+/matches one or more spaces or commas, so it splits on any group of these separators.Step 2: Use flag to remove empty parts
The flagPREG_SPLIT_NO_EMPTYtellspreg_splitto exclude empty strings from the result array.Step 3: Check other options
preg_split('/[ ,]/', $string); splits on single separators but may leave empty parts. preg_split('/[ ,]+/', $string); does not remove empty parts. preg_split('/[ ,]+/', $string, 0); uses 0 as limit which is invalid.Final Answer:
preg_split('/[ ,]+/', $string, -1, PREG_SPLIT_NO_EMPTY); -> Option AQuick Check:
Use + for multiple separators and NO_EMPTY flag [OK]
- Not using + to match multiple separators
- Forgetting PREG_SPLIT_NO_EMPTY to remove blanks
- Using invalid limit parameter
Ready to prove it?
Pass the test and this pattern moves unknown
