TheAlgorithms-PHP/String/ReverseWords.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);
}