siteIcon

Tech Novice Tools

PHP Apps

burgerIcon

Umbday Appwaypig

Are you umb-day?

Instructions

Enter some text and this app will translate it into 'piglatin'.

If you are not yet an expert in this dialect, perhaps this training video will help.

Eureka: Code Insights:

Originally, this code did not process words with 'QU' in an acceptable fashion. We wanted 'queen' to be 'een-quay' and 'squeak' to be 'eak-squay' as it does now.

To do this, we used the strpos function to find the location of 'QU' in a word during conversion. However, it returns the position when found and 'false' when not found. BUT...logically, 'false' equates to '0' so how do you process words that BEGIN with 'QU' (zero-based indexing, remember?)

The code snippet below shows the logic we used: ===

Other functions are shown for your edification. Enjoy!

                                
function makePigLatinWord($w){
    $exemptWords = array("THE", "AN", "OR", "AND", "I", 
    "IS", "ARE", "WAS", "WERE", "SO", "TO", "WHAT", 
    "OF", "IM", "IVE");
    if(strlen($w) <= 2 || in_array($w, $exemptWords)){
        return $w;
    }else{
        $n = countLettersTillVowel($w);
        if ($n == 0){
            return $w."-way";
        }else{
            //deal with words containing a 'QU'
            $locn = strpos($w, "QU");
            //eureka: false and zero are considered 
            //the same; therefore use ===
            if($locn===0 || $locn > 0){
                $prefix = substr($w, 0, $locn+2);
                $suffix = substr($w, $locn+2);
            }else{
                $prefix = substr($w, 0, $n);
                $suffix = substr($w, $n);
            }
            return $suffix."-".$prefix."AY";
        }
        return $w."-ay";
    }  
}//end function makePigLatinWord

function countLettersTillVowel($w){
    $num = 0;
    $letters = str_split($w);
    $capVowels = array("A", "E", "I", "O", "U");
    //https://stackoverflow.com/questions/
    //4601032/php-iterate-on-string-characters
    foreach ($letters as $ltr) {
        if(in_array($ltr, $capVowels)) {
            break;
        }else{
            $num += 1;
        }
    }
    return $num;
}//end function countLettersTillVowel

function removeAllSymbolsButAllowed($phrase, $allowed){
    $origSymbols = arrayFromStr(strtolower($phrase));
    $len = count($origSymbols);
    $mySymbols = array();
    for($ndx = 0; $ndx < $len; $ndx++){
        $symbol = $origSymbols[$ndx];
        if(in_array($symbol, $allowed)){
            array_push($mySymbols, $symbol);
        }
    }//end for loop
    $sanitizedPhrase = strFromArray($mySymbols);
    $sanitizedPhrase = strtoupper($sanitizedPhrase);
    return $sanitizedPhrase;
}//end function removeAllSymbolsButAllowed

function getPigLatinPhrase($phrase){
    $words = explode(' ', $phrase);
    $len = count($words);
    $myPLWords = array();
    for($ndx = 0; $ndx < $len; $ndx++){
        $aWord = $words[$ndx];
        $pl = makePigLatinWord($aWord);
        $pl .= " ";
        array_push($myPLWords, $pl);
    }//end for loop
    $plPhrase = strFromArray($myPLWords);
    $plPhrase = strtoupper($plPhrase);
    return $plPhrase;
}//end function getPigLatinPhrase

//from utilityFunctions.php
function strFromArray($arr){
    $len = count($arr);
    $temp = "";
    for ($k = 0; $k < $len; $k++){
        $char = $arr[$k];
        $temp .= $char;
    }
    return $temp;
}//end function strFromArray                            

Last update: 09/24/19


What's your message?: