Skip to content

Instantly share code, notes, and snippets.

@osbre
Created February 3, 2020 20:43
Show Gist options
  • Select an option

  • Save osbre/a535c6656feacc03203100be3d3e88bd to your computer and use it in GitHub Desktop.

Select an option

Save osbre/a535c6656feacc03203100be3d3e88bd to your computer and use it in GitHub Desktop.
CodeBattle: "Given a sentence, sort characters in each word alphabetically."
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.")
<?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