3v4l.org

run code in 300+ PHP versions simultaneously
<?php function high($input) { // Initialize: Convert the string to lowercase, and generate an array of the alphabet $input = strtolower($input); $alphabet = range('a', 'z'); $values = array_flip($alphabet); // Split the words into an array, and declare initial scores $words = explode(" ", $input); $highestScore = null; $highestIndex = null; // Iterate over each word foreach($words as $k=>$w) { // Calculate the score of the current word // The score is the position of the flipped alphabet array, or 0 if its not in the lsit $score = array_sum(array_map(function($v) use ($values) { return $values[$v] ?? 0; }, str_split($w))); // If the current score is higher than the previous highest, overwrite it if ($highestScore < $score) { $highestScore = $score; $highestIndex = $k; } } // Return the word at the index where the score was the highest // If there were no scores (if $input is empty), return a default message return $words[$highestIndex] ?? 'N/A - No words found'; } var_dump(high('man i need a taxi up to ubud'));

preferences:
53.63 ms | 402 KiB | 5 Q