Sha256: 38faed9151577378726e49ab6067a62d868a19a547860cc3d0638a8274c412b1

Contents?: true

Size: 665 Bytes

Versions: 384

Compression:

Stored size: 665 Bytes

Contents

<?php
/**
 * Translates a string into Pig Latin
 *
 * @param  string $str
 * @return string
 */
function translate($str)
{
    $words = explode(" ", $str);
    $translatedWords = array_map('translateWord', $words);
    return implode(' ', $translatedWords);
}

/**
 * Translates a single word
 *
 * @param  string $str
 * @return string
 */
function translateWord($str)
{
    if (preg_match('/^s?qu|thr?|s?ch/', $str, $match)) {
        return sprintf("%s%say", substr($str, strlen($match[0])), $match[0]);
    }

    if (preg_match('/^[aeiou]|yt|xr/', $str)) {
        return sprintf("%say", $str);
    }

    return sprintf("%s%say", substr($str, 1), $str[0]);
}

Version data entries

384 entries across 384 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/php/exercises/pig-latin/example.php
trackler-2.2.1.179 tracks/php/exercises/pig-latin/example.php
trackler-2.2.1.178 tracks/php/exercises/pig-latin/example.php
trackler-2.2.1.177 tracks/php/exercises/pig-latin/example.php
trackler-2.2.1.176 tracks/php/exercises/pig-latin/example.php
trackler-2.2.1.175 tracks/php/exercises/pig-latin/example.php
trackler-2.2.1.174 tracks/php/exercises/pig-latin/example.php
trackler-2.2.1.173 tracks/php/exercises/pig-latin/example.php
trackler-2.2.1.172 tracks/php/exercises/pig-latin/example.php
trackler-2.2.1.171 tracks/php/exercises/pig-latin/example.php
trackler-2.2.1.170 tracks/php/exercises/pig-latin/example.php
trackler-2.2.1.169 tracks/php/exercises/pig-latin/example.php
trackler-2.2.1.167 tracks/php/exercises/pig-latin/example.php
trackler-2.2.1.166 tracks/php/exercises/pig-latin/example.php
trackler-2.2.1.165 tracks/php/exercises/pig-latin/example.php
trackler-2.2.1.164 tracks/php/exercises/pig-latin/example.php
trackler-2.2.1.163 tracks/php/exercises/pig-latin/example.php
trackler-2.2.1.162 tracks/php/exercises/pig-latin/example.php
trackler-2.2.1.161 tracks/php/exercises/pig-latin/example.php
trackler-2.2.1.160 tracks/php/exercises/pig-latin/example.php