To find the kth longest word in a string in PHP, we can follow these steps:
explode()
function.usort()
function to sort the array of words by their length in descending order.k-1
.Here's the implementation:
main.php545 chars20 lines
In this example, the function kthLongestWord()
takes two arguments: $str
is the input string, and $k
is the desired rank of the longest word. The function returns the desired word as a string.
gistlibby LogSnag