BigToach.com

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($word1) . $word{0} . $sur ' ';
                }
            }
        }
    }else{
        foreach(
$words as $word){
            
$wordlen strlen($word);
            if((
substr($word, ($wordlen $surlen 1)) == 'w' $sur && strpos($vowels$word{0})) || $wordlen < ($surlen)){
                
$end .= substr($word0, ($wordlen $surlen 1)) . ' ';
            }else{
                if(
substr($word, -2) == $sur){
                    
$end .= $word{($wordlen $surlen 1)} . substr($word0, ($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
?>

Added on Jul 8th at 2 am by Scott - 0 Comments


0 Responses to piglatin

Name:

Site or Mail:

Comment:

To try to reduce the ammount of spam, please enter the word GOOSE in the following form

Security Code: