0
0
PHPprogramming~10 mins

String split and explode 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 into an array using a space.

PHP
<?php
$string = "apple banana cherry";
$array = explode([1], $string);
print_r($array);
?>
Drag options to blanks, or click blank then click option'
A" "
B","
C"-"
D";"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a comma instead of a space as delimiter.
Forgetting to put the delimiter inside quotes.
2fill in blank
medium

Complete the code to split the string by commas.

PHP
<?php
$csv = "red,green,blue";
$colors = explode([1], $csv);
print_r($colors);
?>
Drag options to blanks, or click blank then click option'
A","
B" "
C";"
D"|"
Attempts:
3 left
💡 Hint
Common Mistakes
Using space as delimiter when the string uses commas.
Not putting the delimiter in quotes.
3fill in blank
hard

Fix the error in the code to split the string by dash.

PHP
<?php
$dates = "2023-06-15";
$parts = explode([1], $dates);
print_r($parts);
?>
Drag options to blanks, or click blank then click option'
A"_"
B"-"
C'-'
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using the dash without quotes causes a syntax error.
Using the wrong delimiter character.
4fill in blank
hard

Fill both blanks to create an array of words longer than 3 letters.

PHP
<?php
$sentence = "I love coding in PHP";
$words = explode([1], $sentence);
$longWords = array_filter($words, fn($word) => strlen($word) [2] 3);
print_r($longWords);
?>
Drag options to blanks, or click blank then click option'
A" "
B>
C<
D,
Attempts:
3 left
💡 Hint
Common Mistakes
Using comma as delimiter instead of space.
Using less than operator instead of greater than.
5fill in blank
hard

Fill all three blanks to create an associative array of words and their lengths for words longer than 2 letters.

PHP
<?php
$text = "apple banana cat dog";
$result = [];
foreach(explode(" ", $text) as [3]) {
    if(strlen([3]) > 2) {
        $result[[1]] = [2];
    }
}
print_r($result);
?>
Drag options to blanks, or click blank then click option'
A$word
Bstrlen($word)
D$text
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names.
Confusing keys and values in the array.