How to split a string into an array by delimiter and remove empty elements in PHP

1 Answer

0 votes
$s = "C#,Java,,C,,,Python,,,,,C++,,PHP,";
        
$arr = array_filter(explode(',', $s), function($value) {
            return $value !== '';
        });

foreach ($arr as $element) {
    echo $element . PHP_EOL;
}



/*
run:
 
C#
Java
C
Python
C++
PHP
 
*/

 



answered Sep 23, 2024 by avibootz
...