Hello, and welcome! Are you ready to elevate your string manipulation skills in PHP? Today, we'll delve into a task that will bolster your comprehension of strings and enhance your creativity. The task involves splitting a string into words and then reversing each word as if reflected in a mirror. Does that sound interesting? Let's get started!
You're tasked with considering a string filled with words and writing a PHP function that accepts this string. The function should reverse the character order of each word and form a new string consisting of these reversed words.
Here's what you need to keep in mind:
- The input string will contain between 1 and 100 words.
- Each word in the string is a sequence of characters separated by whitespace.
- The characters can range from
a
toz
,A
toZ
,0
to9
, or even an underscore_
. - The provided string will neither start nor end with a space, and double spaces won't be present either.
- After reversing the words, your program should output a single string with the reversed words preserving their original order.
Example
Consider the input string "Hello neat php_lovers_123"
.
The function works as follows:
Hello
becomesolleH
neat
becomestaen
php_lovers_123
becomes321_srevol_php
Afterward, it forms a single string with these reversed words, producing "olleH taen 321_srevol_php"
.
Therefore, if you call reverseWords("Hello neat php_lovers_123")
, the function should return "olleH taen 321_srevol_php"
.
Let's begin breaking this down!
Our first task is to separate the words in the sentence. In PHP, the explode()
function allows us to achieve this easily. The delimiter you'll use in the explode()
function is a single space " "
. Here is a sample code to illustrate this:
php1$inputStr = "Hello neat php_lovers_123"; 2$words = explode(" ", $inputStr); 3 4// Now the array 'words' holds all the words of the string
Note that " "
as the delimiter ensures that the string is split at each space, effectively separating the words.
Next, we need to reverse each word separated in the previous step. In PHP, we can use the strrev()
function to do this. Let's add these lines to our existing code:
php1$reversedWords = []; 2 3foreach ($words as $word) { 4 $reversedWords[] = strrev($word); 5} 6 7// 'reversedWords' now contains the reversed words
Finally, we need to consolidate these reversed words into a single string, separated by spaces. We can achieve this using the implode()
function in PHP. Here's how we do that:
php1$finalStr = implode(" ", $reversedWords);
This joins all the elements of the reversedWords
array into a single string with each word separated by a space.
It remains for us to combine the code from the steps together in a function reverseWords
and test it.
php1<?php 2 3function reverseWords($inputStr) { 4 $words = explode(" ", $inputStr); 5 $reversedWords = []; 6 7 foreach ($words as $word) { 8 $reversedWords[] = strrev($word); 9 } 10 11 return implode(" ", $reversedWords); 12} 13 14// Test the function 15echo reverseWords("Hello neat php_lovers_123"); // prints: 'olleH taen 321_srevol_php' 16 17?>
Well done! By completing this lesson, you've sharpened your proficiency in manipulating strings in PHP. You've improved especially in reversing the order of characters in a word. I hope you're feeling confident and excited about your PHP skills. Remember, mastering these skills requires frequent practice. Therefore, take some time to explore related problems and practice what you’ve learned. Enjoy the journey of learning!