0
0
PHPprogramming~15 mins

Preg_split for splitting in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Splitting Text Using preg_split in PHP
📖 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
Need a 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
Need a 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
Need a hint?

Use preg_split($pattern, $sentence) to split the string.

4
Print the resulting array
Use print_r to print the $words array.
PHP
Need a hint?

Use print_r($words); to display the array.