In this tutorial, you shall learn how to split a string by single space delimiter in PHP using explode() function, with the help of examples.
PHP – Split String by Single Space
To split a string into parts using single space as delimiter or separator in PHP, use explode()
function.
Call explode()
function and pass the single space character (as string), and the original string as arguments. The function returns an array of strings obtained after splitting the given string using single space character as separator string.
Syntax
The syntax of explode()
function to split the $input
string by single space is
explode(' ', $input)
Example
In this example, we take an input string 'apple banana cherry mango'
where the values are separated by single space ' '
. We split this string into an array of values.
PHP Program
<?php
$input = 'apple banana cherry mango';
$output = explode(' ', $input);
foreach ($output as $x) {
echo $x;
echo '<br>';
}
?>
Output
Conclusion
In this PHP Tutorial, we learned how to split a string with single space character as delimiter/separator, using explode()
function.