ClarkeRubber

UNSW ProgComp: Problem 2 - 2006

Jun 9th, 2012
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.70 KB | None | 0 0
  1. <?php
  2. $input = <<<END
  3. Alexandria
  4. Berowra Waters
  5. Campsie
  6. Doonside
  7. East Ryde
  8. Fairfield East
  9. Granville
  10. Heathcote
  11. Ingleburn
  12. Jannali
  13. Kirrawee
  14. Lane Cove North
  15. Maianbar
  16. Neutral Bay
  17. Old Toongabbie
  18. Parramatta
  19. Queenscliff
  20. Raby
  21. Sans Souci
  22. Tempe
  23. Ultimo
  24. Vaucluse
  25. Woolloomooloo
  26. Yagoona
  27. Zetland
  28. END;
  29.  
  30. function remove_vowels($a){
  31.     if(!in_array($a, array('A', 'E', 'I', 'O', 'U', ' '))){
  32.         return $a;
  33.     }
  34. }
  35.  
  36. $input = explode("\n", $input);
  37.  
  38. $unwanted = array('A', 'E', 'I', 'O', 'U', ' ');
  39.  
  40. $max_len = null;
  41.  
  42. foreach($input as $value){
  43.     if(strlen($value) > $max_len){
  44.         $max_len = strlen($value);
  45.     }
  46.     $subject = explode(" ", $value);
  47.     $outword = null;
  48.     foreach($subject as $value2){
  49.         $word_array = null;
  50.         $string_array = str_split($value2);
  51.  
  52.         $first_character = $string_array[0];
  53.         if(!in_array($first_character, $unwanted)){
  54.             $first_character = '';
  55.         }
  56.         //$last_two_vowels = str_replace("#([aeiou]{2}|*)#") that isn't going to work
  57.         if(in_array(strtoupper($string_array[count($string_array)-2]), $unwanted) && in_array(strtoupper($string_array[count($string_array)-1]), $unwanted)){
  58.             //this means that both of the characters are a vowel,
  59.             // therefor they must not be removed, or more precisely, they will later be added to the end
  60.             $end_vowels = $string_array[count($string_array)-2].$string_array[count($string_array)-1];
  61.         }else{
  62.             $end_vowels = '';
  63.         }
  64.         foreach($string_array as $char){
  65.             if(!in_array(strtoupper($char), $unwanted)){
  66.                 $word_array[] = $char;
  67.             }
  68.         }
  69.         $outword[] = $first_character.implode($word_array).$end_vowels;
  70.     }
  71.     $output[] = implode($outword);
  72. }
  73.  
  74. foreach($input as $key => $value){
  75.     echo str_pad($value, $max_len+1).$output[$key]."\n";
  76. }
Advertisement
Add Comment
Please, Sign In to add comment