Complete the code to split the string by commas using preg_split.
<?php $string = "apple,banana,cherry"; $parts = preg_split([1], $string); print_r($parts); ?>
The pattern /,/ matches commas, so preg_split splits the string at commas.
Complete the code to split the string by one or more spaces using preg_split.
<?php $string = "one two three"; $words = preg_split([1], $string); print_r($words); ?>
The pattern /\s+/ matches one or more whitespace characters, so preg_split splits the string at spaces.
Fix the error in the code to split the string by dots using preg_split.
<?php $string = "www.example.com"; $parts = preg_split([1], $string); print_r($parts); ?>
The dot . is a special character in regex, so it must be escaped as \. to match a literal dot.
Complete the code to split the string by commas or semicolons using preg_split.
<?php $string = "red,green;blue,yellow;orange"; $colors = preg_split([1], $string); print_r($colors); ?>
The pattern /[,;]/ matches either a comma or a semicolon, so preg_split splits the string at both.
Complete the code to split the string by commas, semicolons, or spaces using preg_split.
<?php $string = "cat, dog;bird fish"; $animals = preg_split([1], $string); print_r($animals); ?>
The pattern /[ ,;]+/ matches one or more spaces, commas, or semicolons, splitting the string at any of these characters.