mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 15:18:13 +01:00
13 lines
287 B
PHP
13 lines
287 B
PHP
<?php
|
|
|
|
function reverse_words(string $text)
|
|
{
|
|
$text = trim($text);
|
|
$words = explode(' ', $text);
|
|
$reversedWords = [];
|
|
for ($i = (count($words) - 1); $i >= 0; $i--) {
|
|
$reversedWords[] = $words[$i];
|
|
}
|
|
return implode(' ', $reversedWords);
|
|
}
|