Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. <?php
  2. class VowelFinder extends Model
  3. {
  4.  
  5. // This function prints unique vowels (A, E, I, O, U)
  6. // from a given string in the order they appear.
  7. // A 'Y' is considered a consonant if it begins a string and this string is greater than 1 character.
  8. // Otherwise, a 'Y' is considered a vowel.
  9.  
  10. public function findVowels($string = '') {
  11.  
  12. $string = $this->removeFirstY($string);
  13. $string = $this->leaveOnlyVowels($string);
  14. $string = $this->removeDublicateCharacters($string);
  15.  
  16. return $string;
  17. }
  18.  
  19.  
  20. private function removeFirstY($string) {
  21. if(strlen($string) > 1) {
  22. string = preg_replace("/^y?/i", "", $string);
  23. }
  24.  
  25. return $string;
  26. }
  27.  
  28.  
  29. private function leaveOnlyVowels($string) {
  30. $pattern = "/[^aeiouy]?/i";
  31. $string = preg_replace($pattern, '', $string);
  32.  
  33. return $string;
  34. }
  35.  
  36. private function removeDublicateCharacters($string){
  37. return implode(array_unique(str_split($string)));
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement