Last active
September 25, 2020 01:07
-
-
Save ogabrielguerra/e76d82db6de91896d2d304ca8b7e28c9 to your computer and use it in GitHub Desktop.
Sort Special Words
This file contains 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 sortWords(String $string){ | |
$words = explode(' ', $string); | |
$numWords = count($words); | |
$englishWords = []; | |
$chineseWords = []; | |
for($i=0; $i<$numWords; $i++){ | |
if(ctype_alnum($words[$i])){ | |
if(!in_array($words[$i], $englishWords, true)) { | |
array_push($englishWords, $words[$i]); | |
} | |
}else{ | |
array_push($chineseWords, $words[$i]); | |
} | |
} | |
return implode(' ', $englishWords) . ' ' . implode(' ', array_reverse($chineseWords)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment