Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class VowelFinder extends Model
- {
- // This function prints unique vowels (A, E, I, O, U)
- // from a given string in the order they appear.
- // A 'Y' is considered a consonant if it begins a string and this string is greater than 1 character.
- // Otherwise, a 'Y' is considered a vowel.
- public function findVowels($string = '') {
- $string = $this->removeFirstY($string);
- $string = $this->leaveOnlyVowels($string);
- $string = $this->removeDublicateCharacters($string);
- return $string;
- }
- private function removeFirstY($string) {
- if(strlen($string) > 1) {
- string = preg_replace("/^y?/i", "", $string);
- }
- return $string;
- }
- private function leaveOnlyVowels($string) {
- $pattern = "/[^aeiouy]?/i";
- $string = preg_replace($pattern, '', $string);
- return $string;
- }
- private function removeDublicateCharacters($string){
- return implode(array_unique(str_split($string)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement