How to move the first word to the end of a string in PHP

1 Answer

0 votes
function move_first_word_to_end_of_string($s) {
    // Split into words (like Python's split())
    $parts = preg_split('/\s+/', trim($s));

    if (count($parts) <= 1) {
        return trim($s);
    }

    // Move first word to the end
    $first = array_shift($parts);
    $parts[] = $first;

    return implode(' ', $parts);
}

$s = "Would you like to know more? (Explore and learn)";
$result = move_first_word_to_end_of_string($s);

echo $result;




/*
run:

you like to know more? (Explore and learn) Would

*/

 



answered Feb 5 by avibootz
...