![]() |
Reversing the even-length words of a string is a common programming task that involves manipulating individual words within a sentence. In PHP, there are several approaches to achieve this, each offering different levels of complexity and flexibility. Table of Content Using Explode and Implode FunctionsThe basic approach is to split the string into an array of words using the explode() function, reverse the even-length words, and then reconstruct the string using the implode function. Example: Here, we use explode to split the input string into an array of words, iterate through the words, reverse the even-length ones with strrev() function, and finally join the modified words back into a string using the implode() function.
Output Welcome ot skeeGskeeG Using Regular ExpressionsRegular expressions provide a concise way to match and manipulate text patterns. We can use a regular expression to identify even-length words and then reverse them. Example: Here, the preg_replace_callback() function will return the string containing all the matched expression that are getting replaced with the substring returned by the callback function. For getting the string length, the strlen() function is used. Then implementing the strrev() function to reverse the string.
Output Welcome ot skeeGskeeG Using Array Map and Array WalkAnother approach to reversing even-length words in a string is by utilizing array_map() or array_walk() functions in PHP. This method is both functional and easy to understand. Example:
Output Sam si working yrev drah |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |