3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = '___RN___RN___R___N___RN___RN'; $del = array('RN', 'R', 'N'); # This won't work if delimiters 2-n include characters from delimiter 1 var_dump(explode( $del[0], str_replace($del, $del[0], $str))); # This, however, will work function array_explode($delimiters, $string){ if(!is_array(($delimiters)) && !is_array($string)){ //if neither the delimiter nor the string are arrays return explode($delimiters,$string); } else if(!is_array($delimiters) && is_array($string)) { //if the delimiter is not an array but the string is foreach($string as $item){ foreach(explode($delimiters, $item) as $sub_item){ $items[] = $sub_item; } } return $items; } else if(is_array($delimiters) && !is_array($string)) { //if the delimiter is an array but the string is not $string_array[] = $string; foreach($delimiters as $delimiter){ $string_array = array_explode($delimiter, $string_array); } return $string_array; } } var_dump(array_explode($del,$str));
Output for 5.6.38 - 5.6.40, 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.26, 8.3.0 - 8.3.15, 8.4.1 - 8.4.2
array(11) { [0]=> string(4) "___R" [1]=> string(0) "" [2]=> string(4) "___R" [3]=> string(0) "" [4]=> string(4) "___R" [5]=> string(3) "___" [6]=> string(4) "___R" [7]=> string(0) "" [8]=> string(4) "___R" [9]=> string(0) "" [10]=> string(0) "" } array(7) { [0]=> string(3) "___" [1]=> string(3) "___" [2]=> string(3) "___" [3]=> string(3) "___" [4]=> string(3) "___" [5]=> string(3) "___" [6]=> string(0) "" }

preferences:
90.75 ms | 409 KiB | 5 Q