PHP - 将句子中的第一个和第三个单词交换为 3 个单词

PHP - swap the first and third word in a sentence with 3 words

如果我有一个输入:

$input = "One Two Three";

如何将第一个单词与第三个单词交换并得到以下输出:

"Three Two One"

这是你的答案。

<?php
    $input = "One Two Three";
    $array = explode(' ',$input);
    krsort($array);
    $input = implode(' ',$array);
    echo $input;
?>

试试这个:

$input = "One Two Three";
$words = str_word_count($input, 1);
$reversed_words = array_reverse($words);
print_r($reversed_words); // prints Array ( [0] => Three [1] => Two [2] => One )

创建字符串:

$input = implode(' ', $reversed_words);
echo $input; // "Three Two One"