August 17, 2024
PHP explode function
explode() function is one of the most important PHP’s built in function of string. Sometimes it happens that you want to convert a string's element into an array so for the same you can use explode() function in PHP. explode() function requires two string arguments first is a separator string and second whole string from which you will create an array. The explode() function divide the string in small chunk through the separator string and write in an array element. Every time explode() function found the separator string it creates an new array element. The example of explode() function is mentioned below:
Code
<?php
$weekDaysString = "Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday";
$weekDaysArray = explode(", ",$weekDaysString);
print_r($weekDaysArray);
?>
In above mentioned example we have a string variable with the name of $weekDaysString which have all the week days name in comma separated format. In the next line we have use explode function with $weekDaysString string variable and provide separator string as comma(,) then explode function returns an array of all the week days and we store it in $weekDaysArray and print it on the screen through print_r function.