PHP
php.net โบ manual โบ en โบ function.explode.php
PHP: explode - Manual
If separator values appear at the start or end of string, said values will be added as an empty array value either in the first or last position of the returned array respectively. ... <?php // Example 1 $pizza = "piece1 piece2 piece3 piece4 piece5 piece6"; $pieces = explode(" ", $pizza); echo $pieces[0], PHP_EOL; // piece1 echo $pieces[1], PHP_EOL; // piece2 // Example 2 $data = "foo:*:1023:1000::/home/foo:/bin/sh"; list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data); echo $user, PHP_EOL; // foo echo $pass, PHP_EOL; // * ?>
W3Schools
w3schools.com โบ php โบ func_string_explode.asp
PHP explode() Function
<?php $str = "Hello world. It's a beautiful day."; print_r (explode(" ",$str)); ?> Try it Yourself ยป ยท The explode() function breaks a string into an array.
php - Explode to array and print each element as list item - Stack Overflow
I have a set of numbers in a table field in database, the numbers are separated by comma ','. I am trying to do the following: Step 1. : SELECT set of numbers from database and explode it to array... More on stackoverflow.com
php - Explode each values of Array element - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I want to explode each element of an array and return a new array using array_map, so that I can avoid using loop, and creating extra function to call back. More on stackoverflow.com
PHP explode array then loop through values and output to variable - Stack Overflow
What it takes to be a player in the international AI... ... Help Shape the 2026 Developer Survey! ... 2 Split a delimited string and create single-element associative rows from the values with a static key ยท 0 PHP loop through exploded values to populate an array More on stackoverflow.com
php - Exploding by Array of Delimiters - Stack Overflow
Is there any way to explode() using an array of delimiters? PHP Manual: array explode ( string $delimiter , string $string [, int $limit ] ) Instead of using string $delimiter is there any way to... More on stackoverflow.com
04:44
PHP String Functions: Explode, Implode & Str_split Explained for ...
01:28
PHP Explode Function | How to split a string into an array? | Code ...
17:18
PHP - Turn Strings into Arrays with explode() - YouTube
04:24
What is explode() Function within 4 Minutes - PHP Tutorial - YouTube
10:04
PHP Implode and Explode Function in Hindi | PHP Tutorial in Hindi ...
06:25
PHP Tutorial (& MySQL) #28 - The Explode Function - YouTube
Zero To Mastery
zerotomastery.io โบ blog โบ php-explode-beginners-guide
Beginner's Guide to PHP explode() Function (With Code Examples!) | Zero To Mastery
May 13, 2024 - As I mentioned earlier, the explode() function can also take an optional third argument, limit, so letโs take a look at this in action. If limit is set, the returned array will contain a maximum of limit elements with the last element containing the rest of the string. ... <?php $inputString = "apple, banana, cherry, date, elderberry"; print_r(explode(", ", $inputString, 2)); ?>
Codecademy
codecademy.com โบ docs โบ php โบ string functions โบ explode()
PHP | String Functions | explode() | Codecademy
June 27, 2025 - The explode() function is a built-in PHP string function that splits a string into an array based on a specified delimiter. It takes a string and breaks it apart at every occurrence of the delimiter, returning an array containing the resulting ...
Reintech
reintech.io โบ blog โบ comprehensive-guide-php-explode-function
A Comprehensive Guide to the PHP `explode()` Function
... Positive value: Returns at ... case involves splitting comma-separated values: $languages = "PHP, JavaScript, Python, Ruby"; $languageArray = explode(", ", $languages); print_r($languageArray); // Output: // Array // ( // [0] => PHP // [1] => JavaScript // [2] => Python ...
Top answer 1 of 6
25
$numbers = '1,2,3';
$array = explode(',', $numbers);
foreach ($array as $item) {
echo "<li>$item</li>";
}
2 of 6
2
Assuming your original $set_of_numbers is simply a CSV string, something like 1,2,3,4,..., then your foreach is "mostly" ok. But your variable naming is quite bonkers, and your print-r() call uncesary:
$array = explode(',', $set_of_numbers);
foreach($array as $key => $value) {
echo "<li>$key: $value</li>";
}
Assuming that 1,2,3,4... string, you'd get
<li>0: 1</li>
<li>1: 2</li>
<li>2: 3</li>
etc...
Top answer 1 of 4
8
You can use
$result = array_map(function($val) {
return explode(',', $val);
}, $input);
Which will result in
Array
(
[2] => Array
(
[0] => 2
[1] => 6
)
[3] => Array
(
[0] => 1
)
[4] => Array
(
[0] => 14
)
[5] => Array
(
[0] => 10
)
[6] => Array
(
[0] => 8
)
)
This will work on PHP >= 5.3 with support for anonymous functions.
2 of 4
4
You can also do
$result = array_map('str_getcsv', $input);
This will also result in
Array
(
[2] => Array
(
[0] => 2
[1] => 6
)
[3] => Array
(
[0] => 1
)
[4] => Array
(
[0] => 14
)
[5] => Array
(
[0] => 10
)
[6] => Array
(
[0] => 8
)
)
Top answer 1 of 5
31
In your code you are overwritting the $categories variable in each iteration. The correct code would look like:
$categories = '';
$cats = explode(",", $item['category_names']);
foreach($cats as $cat) {
$cat = trim($cat);
$categories .= "<category>" . $cat . "</category>\n";
}
update: as @Nanne suggested, explode only on ','
2 of 5
2
Without a for loop
$item['category_names'] = "Hair, Fashion, News";
$categories = "<category>".
implode("</category>\n<category>",
array_map('trim', explode(",", $item['category_names']))) .
"</category>\n";
echo $categories;
Scientech Easy
scientecheasy.com โบ home โบ blog โบ php explode() function to split string into array
PHP explode() Function to Split String into Array - Scientech Easy
May 28, 2025 - The explode() function returns an array of strings created by splitting the original string at each point where the separator occurs. If the separator is an empty string (โ โ), the explode() function throws a ValueError starting from PHP 8.0. [blocksy-content-block id=โ12371โ] If the ...
3D Bay
clouddevs.com โบ home โบ php guides โบ unpacking phpโs explode() function: a complete guide
Unpacking PHP's explode() Function: A Complete Guide
December 13, 2023 - As you can see, explode() has successfully split the string into an array, with each element corresponding to a fruit. The delimiter is a crucial element when using explode(). It determines how the input string should be divided. Letโs explore different scenarios involving delimiters. In most cases, youโll use single characters as delimiters. Hereโs an example: php $input = "one,two,three,four,five"; $numbers = explode(",", $input); print_r($numbers);
Scaler
scaler.com โบ home โบ topics โบ php explode() function
PHP explode() Function - Scaler Topics
March 18, 2024 - In PHP, "explode" is a built-in function that allows you to split a string into an array of substrings based on a specified delimiter. The delimiter can be any character or sequence of characters, such as a comma, space, or a period.