Просто идея:
function explode_reversed($delim,$s){
$result = array();
$w = "";
$l = 0;
for ($i = strlen($s)-1; $i>=0; $i-- ):
if ( $s[$i] == "$delim" ):
$l++;
$w = "";
else:
$w = $s[$i].$w;
endif;
$result[$l] = $w;
endfor;
return $result;
}
$arr = explode_reversed(" ","Hello! My name is John.");
print_r($arr);
Результат:
Array
(
[0] => John.
[1] => is
[2] => name
[3] => My
[4] => Hello!
)
Но это гораздо медленнее, чем взорваться. Сделал тест:
$start_time = microtime(true);
for ($i=0; $i<1000;$i++)
$arr = explode_reversed(" ","Hello! My name is John.");
$time_elapsed_secs = microtime(true) - $start_time;
echo "time: $time_elapsed_secs s<br>";
Занимает 0,0625 - 0,078125 с
Но
for ($i=0; $i<1000;$i++)
$arr = explode(" ","Hello! My name is John.");
Занимает всего 0,015625 с
Похоже, самое быстрое решение:
array_reverse(explode($your_delimiter, $your_string));
В цикле 1000 раз это лучшее время, которое я могу получить 0,03125 с.
person
John Boe
schedule
19.10.2019