Skip to content

Instantly share code, notes, and snippets.

@CodeBySwati
Last active December 19, 2024 16:06
Show Gist options
  • Save CodeBySwati/28289830178ae861c6f83c72f02bffae to your computer and use it in GitHub Desktop.
Save CodeBySwati/28289830178ae861c6f83c72f02bffae to your computer and use it in GitHub Desktop.
Create a script in php that translates the input into PigLatin. 1. If a word starts with a consonant, put the first letter of the word at the end of the word and add "ay." ex: happy = appyh + ay = appyhay

PHP script that translates the input into PigLatin.

1. If a word starts with a consonant, put the first letter of the word at the end of the word and add "ay."

ex: happy = appyh + ay = appyhay

Here's a PHP script that translates input text into Pig Latin:

<?php
function toPigLatin($input) {
    // Split the input into words
    $words = explode(' ', $input);
    $translatedWords = [];

    foreach ($words as $word) {
        $word = strtolower($word); // Convert to lowercase for simplicity
        if (preg_match('/^[aeiou]/i', $word)) {
            // If word starts with a vowel, simply add "yay" to the end
            $translatedWords[] = $word . 'yay';
        } else {
            // If word starts with a consonant, find the first vowel
            preg_match('/^[^aeiou]+/', $word, $matches);
            $consonantCluster = $matches[0]; // Get the consonant cluster
            $restOfWord = substr($word, strlen($consonantCluster)); // Rest of the word
            $translatedWords[] = $restOfWord . $consonantCluster . 'ay';
        }
    }

    // Join the translated words back into a string
    return implode(' ', $translatedWords);
}

// Example usage:
$input = "happy coding world";
echo "Original: $input\n";
echo "Pig Latin: " . toPigLatin($input) . "\n";
?>

Explanation:

  1. Word Splitting: The input string is split into individual words using explode().
  2. Checking the Starting Letter: Words starting with vowels (a, e, i, o, u) simply get "yay" appended. Words starting with consonants are modified by moving the leading consonant (or cluster of consonants) to the end of the word and appending "ay".
  3. Regular Expressions: /^[aeiou]/i: Matches words starting with a vowel.
    /^[^aeiou]+/: Matches the leading consonant(s) of a word.
  4. Joining Translated Words: Finally, the translated words are joined back into a sentence using implode().

Example Output:

For the input "happy coding world", the script outputs:

Original: happy coding world  
Pig Latin: appyhay odingcay orldway
<?php
function toPigLatin($input) {
// Split the input into words
$words = explode(' ', $input);
$translatedWords = [];
foreach ($words as $word) {
$word = strtolower($word); // Convert to lowercase for simplicity
if (preg_match('/^[aeiou]/i', $word)) {
// If word starts with a vowel, simply add "yay" to the end
$translatedWords[] = $word . 'yay';
} else {
// If word starts with a consonant, find the first vowel
preg_match('/^[^aeiou]+/', $word, $matches);
$consonantCluster = $matches[0]; // Get the consonant cluster
$restOfWord = substr($word, strlen($consonantCluster)); // Rest of the word
$translatedWords[] = $restOfWord . $consonantCluster . 'ay';
}
}
// Join the translated words back into a string
return implode(' ', $translatedWords);
}
// Example usage:
$input = "happy coding world";
echo "Original: $input\n";
echo "Pig Latin: " . toPigLatin($input) . "\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment