PHP Snippets
piglatin
Information
(PHP Version >= 3)Functions used in this snippet: strlen, explode, strpos, substr.
Description
str piglatin( str str[, bool from] )
Translates str into piglatin. If from is set to true, str will be translated from piglatin into plaintext.
Snippet
<?php
function piglatin($str, $from=false){
$vowels = 'aeiouyAEIOUY';
$letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$end .= '';
$sur = 'ay';
$surlen = strlen($sur);
$words = explode(' ', $str);
if(!$from){
foreach($words as $word){
if(strpos($letters, $word{0})){
if(strpos($vowels, $word{0}) || strlen($word) < 3){
$end .= $word . 'w' . $sur . ' ';
}else{
$end .= substr($word, 1) . $word{0} . $sur . ' ';
}
}
}
}else{
foreach($words as $word){
$wordlen = strlen($word);
if((substr($word, ($wordlen - $surlen - 1)) == 'w' . $sur && strpos($vowels, $word{0})) || $wordlen < (4 + $surlen)){
$end .= substr($word, 0, ($wordlen - $surlen - 1)) . ' ';
}else{
if(substr($word, -2) == $sur){
$end .= $word{($wordlen - $surlen - 1)} . substr($word, 0, ($wordlen - $surlen - 1)) . ' ';
}else{
$end .= $word . ' ';
}
}
}
}
return $end;
}
?>
Example
piglatin Can be used in the following way:
<?php
echo piglatin('oh my goodness its piglatin');
//ohway myway oodnessgay itsway iglatinpay
echo piglatin('ohway myway oodnessgay itsway iglatinpay', true);
// oh my goodness its piglatin
?>
0 Responses to piglatin