mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 07:08:13 +01:00
22 lines
419 B
PHP
22 lines
419 B
PHP
<?php
|
|
|
|
/**
|
|
* This function returns a given sentence with its words
|
|
* in reverse order
|
|
*
|
|
* @param string $text
|
|
* @return string
|
|
*/
|
|
function reverseWords(string $text)
|
|
{
|
|
$text = trim($text);
|
|
$words = explode(' ', $text);
|
|
$reversedWords = [];
|
|
|
|
for ($i = (count($words) - 1); $i >= 0; $i--) {
|
|
$reversedWords[] = $words[$i];
|
|
}
|
|
|
|
return implode(' ', $reversedWords);
|
|
}
|