0
0
PHPprogramming~10 mins

Preg_split for splitting in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to split the string by commas using preg_split.

PHP
<?php
$string = "apple,banana,cherry";
$parts = preg_split([1], $string);
print_r($parts);
?>
Drag options to blanks, or click blank then click option'
A"/,/"
B"/\./"
C"/\s+/"
D"/;/"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a semicolon instead of a comma in the pattern.
Not enclosing the pattern in slashes.
2fill in blank
medium

Complete the code to split the string by one or more spaces using preg_split.

PHP
<?php
$string = "one  two   three";
$words = preg_split([1], $string);
print_r($words);
?>
Drag options to blanks, or click blank then click option'
A"/,/"
B"/\s+/"
C"/\./"
D"/;/"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a comma instead of whitespace in the pattern.
Not using the plus sign to match multiple spaces.
3fill in blank
hard

Fix the error in the code to split the string by dots using preg_split.

PHP
<?php
$string = "www.example.com";
$parts = preg_split([1], $string);
print_r($parts);
?>
Drag options to blanks, or click blank then click option'
A"/./"
B"/\./\"
C"/\\./"
D"/\./"
Attempts:
3 left
💡 Hint
Common Mistakes
Not escaping the dot, which matches any character.
Using incorrect escaping that causes syntax errors.
4fill in blank
hard

Complete the code to split the string by commas or semicolons using preg_split.

PHP
<?php
$string = "red,green;blue,yellow;orange";
$colors = preg_split([1], $string);
print_r($colors);
?>
Drag options to blanks, or click blank then click option'
A"/;/"
B"/\s+/"
C"/[,;]/"
D"/,/"
Attempts:
3 left
💡 Hint
Common Mistakes
Using separate patterns for commas and semicolons.
Not using square brackets to combine characters.
5fill in blank
hard

Complete the code to split the string by commas, semicolons, or spaces using preg_split.

PHP
<?php
$string = "cat, dog;bird fish";
$animals = preg_split([1], $string);
print_r($animals);
?>
Drag options to blanks, or click blank then click option'
A"/[ ,;]+/"
B"/\s/"
C"/;/"
D"/,/"
Attempts:
3 left
💡 Hint
Common Mistakes
Using separate patterns for each delimiter.
Not including the plus sign to match multiple delimiters in a row.