📖 Scenario: You have a sentence with words separated by commas and spaces. You want to split this sentence into individual words.
🎯 Goal: Learn how to use preg_split in PHP to split a string by a pattern (comma and space).
📋 What You'll Learn
Create a string variable with the exact sentence
Create a pattern variable for splitting by comma and space
Use preg_split with the pattern and string
Print the resulting array of words
💡 Why This Matters
🌍 Real World
Splitting text is common when processing data like CSV files, user input, or logs.
💼 Career
Knowing how to split strings using regular expressions helps in data cleaning, parsing, and preparing data for analysis or display.
Progress0 / 4 steps
1
Create the sentence string
Create a string variable called sentence with the exact value "apple, banana, cherry, date".
PHP
Hint
Use double quotes to create the string exactly as shown.
2
Create the pattern for splitting
Create a string variable called pattern with the exact value "/,\s+/" to match a comma followed by one or more spaces.
PHP
Hint
The pattern /,\s+/ means split at a comma and one or more spaces.
3
Split the sentence using preg_split
Use preg_split with $pattern and $sentence to create an array called words.
PHP
Hint
Use preg_split($pattern, $sentence) to split the string.
4
Print the resulting array
Use print_r to print the $words array.
PHP
Hint
Use print_r($words); to display the array.
Practice
(1/5)
1. What does the PHP function preg_split do?
easy
A. Counts the number of words in a string.
B. Joins array elements into a string.
C. Replaces parts of a string matching a pattern.
D. Splits a string into parts using a regular expression pattern.
Solution
Step 1: Understand the function purpose
preg_split is 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 matches preg_split functionality.
Final Answer:
Splits a string into parts using a regular expression pattern. -> Option D
Quick Check:
preg_split splits string by regex pattern = A [OK]
Hint: Remember: preg_split uses regex to split strings [OK]
Common Mistakes:
Confusing preg_split with implode or join functions
Thinking preg_split replaces text instead of splitting
Assuming preg_split counts words
2. Which of the following is the correct syntax to split a string by commas using preg_split?
easy
A. preg_split('/,/', $string);
B. preg_split(',', $string);
C. preg_split($string, '/,/');
D. preg_split($string, ',');
Solution
Step 1: Identify correct parameter order
The first parameter of preg_split is 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 A
Quick Check:
Pattern first with slashes, then string = B [OK]
Hint: Pattern goes first with slashes, then the string [OK]
The pattern passed is a plain space character without regex delimiters, which is invalid syntax for preg_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 B
Quick Check:
Regex pattern needs delimiters like slashes [OK]
Hint: Always wrap regex pattern in delimiters like slashes [OK]
Common Mistakes:
Using plain string without delimiters as pattern
Assuming preg_split works like explode for spaces
Blaming print_r for output issues
5. You want to split a string by any number of spaces or commas, but ignore empty parts in the result. Which preg_split call achieves this?
hard
A. preg_split('/[ ,]+/', $string, -1, PREG_SPLIT_NO_EMPTY);
B. preg_split('/[ ,]/', $string);
C. preg_split('/[ ,]+/', $string);
D. preg_split('/[ ,]+/', $string, 0);
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 flag PREG_SPLIT_NO_EMPTY tells preg_split to 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 A
Quick Check:
Use + for multiple separators and NO_EMPTY flag [OK]
Hint: Use + in pattern and PREG_SPLIT_NO_EMPTY flag [OK]
Common Mistakes:
Not using + to match multiple separators
Forgetting PREG_SPLIT_NO_EMPTY to remove blanks
Using invalid limit parameter
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.