Created
February 3, 2020 20:43
-
-
Save osbre/a535c6656feacc03203100be3d3e88bd to your computer and use it in GitHub Desktop.
CodeBattle: "Given a sentence, sort characters in each word alphabetically."
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Examples: | |
| "Eey fo Entw, adn Eot fo Fgor, Loow fo Abt, adn Egnotu fo Dgo." == solution("Eye of Newt, and Toe of Frog, Wool of Bat, and Tongue of Dog.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function solution($sentence){ | |
| return $sentence = preg_replace_callback( | |
| "/(\w+)/", | |
| function(array $matches) {; | |
| $letters = str_split($matches[0]); | |
| natcasesort($letters); | |
| $string = implode('', $letters); | |
| if (preg_match('/[A-Z]/', $string)) { | |
| $string = ucfirst(strtolower($string)); | |
| } | |
| return $string; | |
| }, | |
| $sentence | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment