BigToach.com

PHP Snippets

« 1 2 3 4 » ... 5

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


morsecode

Information

(PHP Version >= 4.1.0)
Functions used in this snippet: strtolower, strlen, array_key_exists, explode, array_search, str_replace.

Description

str morsecode( str str[, bool fromalse] )

Takes str and converts it into morse code. If the optional from is set to true, the str will changed from morse code to plaintext.

Snippet

<?php
function morsecode($str,$from=false){
    
$morse = array(    'a' => '·-'
                    
'b' => '-···'
                    
'c' => '-·-·'
                    
'd' => '-··'
                    
'e' => '·'
                    
'f' => '··-·'
                    
'g' => '--·'
                    
'h' => '····'
                    
'i' => '··'
                    
'j' => '·---'
                    
'k' => '-·-'
                    
'l' => '·-··'
                    
'm' => '--'
                    
'n' => '-·'
                    
'o' => '---'
                    
'p' => '·--·'
                    
'q' => '--·-'
                    
'r' => '·-·'
                    
's' => '···'
                    
't' => '-'
                    
'u' => '··-'
                    
'v' => '···-'
                    
'w' => '·--'
                    
'x' => '-··-'
                    
'y' => '-·--'
                    
'z' => '--··'
                    
' ' => '   '
                    
'1' => '·----'
                    
'2' => '··---'
                    
'3' => '···--'
                    
'4' => '····-'
                    
'5' => '·····'
                    
'6' => '-····'
                    
'7' => '--···'
                    
'8' => '---··'
                    
'9' => '----·'
                    
'0' => '-----'
                    
'.' => '·-·-·-'
                    
',' => '--··--'
                    
'?' => '··--··'
                    
"'" => '·----·'
                    
'!' => '-·-·--'
                    
'/' => '-··-·'
                    
'(' => '-·--·-'
                    
')' => '-·--·-'
                    
':' => '---···'
                    
';' => '-·-·-·'
                    
'=' => '-···-'
                    
'-' => '-····-'
                    
'_' => '··--·-'
                    
'"' => '·-··-·'
                    
'@' => '·--·-·');
    
$end '';
    
$str strtolower($str);
    if(!
$from){
        for(
$i=0$i<strlen($str); $i++){
            
$end .= array_key_exists($str{$i}, $morse) ? $morse[$str{$i}] . ' ' '';
        }
    }else{
        
$code explode(' '$str);
        foreach(
$code as $value){
            
$key array_search($value$morse);
            
$end .= $key === false '' $key;
            
$end .= $value === '' ' ' '';
        }
        
$end str_replace('    '' '$end);
    }
    return 
$end;
}
?>

Example

morsecode Can be used in the following way:

<?php
echo morsecode('oh my goodness its morsecode!!');
// --- ····     -- -·--     --· --- --- -·· -· · ··· ···     ·· - ···     -- --- ·-· ··· · -·-· --- -·· · -·-·-- -·-·-- 

echo morsecode('--- ····     -- -·--     --· --- --- -·· -· · ··· ···     ·· - ···     -- --- ·-· ··· · -·-· --- -·· · -·-·-- -·-·-- 'true);
// oh my goodness its morsecode!!
?>

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


numtostr

Information

(PHP Version >= 4.1.0)
Functions used in this snippet: strrev, strlen, substr, array_reverse, sizeof, array_key_exists, str_replace, implode.

Description

str numtostr( int num )

Transform the number num into the textual representation of the number.

Snippet

<?php
function numtostr($num){
    
$big = array(    ''
                    
'thousand'
                    
'million'
                    
'billion'
                    
'trillion'
                    
'quadrillion'
                    
'quintillion'
                    
'sextillion'
                    
'septillion');
    
$small = array(    ''
                    
'one'
                    
'two'
                    
'three'
                    
'four'
                    
'five'
                    
'six'
                    
'seven'
                    
'eight'
                    
'nine'
                    
'ten'
                    
'eleven'
                    
'twelve'
                    
'thirteen'
                    
'fourteen'
                    
'fifteen'
                    
'sixteen'
                    
'seventeen'
                    
'eighteen'
                    
'nineteen');
    
$other = array(    '',
                    
'',
                    
'twenty'
                    
'thirty'
                    
'fourty'
                    
'fifty'
                    
'sixty'
                    
'seventy'
                    
'eighty'
                    
'ninety');
    
$hun 'hundred';
    
$end = array();
    
$num strrev($num);
    
$final = array();
    for(
$i=0$i<strlen($num); $i+=3){
        
$end[$i] = strrev(substr($num$i3));
    }
    
$end array_reverse($end);
    for(
$i=0$i<sizeof($end); $i++){
        
$len strlen($end[$i]);
        
$temp $end[$i];
        if(
$len == 3){
            
$final[] = $temp{0} != '0' $small[$end[$i]{0}] . ' ' $hun $small[$end[$i]{0}];
            
$end[$i] = substr($end[$i], 12);
        }
        if(
$len 1){
            
$final[] = array_key_exists($end[$i], $small) ? $small[$end[$i]] : $other[$end[$i]{0}] . ' ' $small[$end[$i]{1}];
        }else{
            
$final[] = $small[$end[$i]{0}];
        }
        
$final[] = $temp != '000' $big[sizeof($end) - $i 1] : '';
    }
    return 
str_replace(array('  ''  ''  ''  ''  ''  ''  '), ' 'implode(' ',$final));
}
?>

Example

numtostr Can be used in the following way:

<?php
echo numtostr(123);
// one hundred twenty three 

echo numtostr(99999999);
//ninety nine million nine hundred ninety nine thousand nine hundred ninety nine 
?>

Added on Jul 7th at 7 pm by Scott - 2 Comments


image_contrast

Information

(PHP Version >= 4.0.6)
Functions used in this snippet: imagesx, imagesy, imagecreatetruecolor, imagecolorat, min, floor, max, ceil, imagesetpixel, imagecolorallocate.

Description

int image_contrast( int img, int percent )

Adjusts the contrast of the image img based on the percent of percent. Percent is any number above 0. The lower the number, the darker the image will get. The higher the number, the brighter the image will get.

*Note: This function is fairly slow, so it would be best to apply the function once and save the image for further viewings.

You can view an example of this at Example of image_contrast

Snippet

<?php
function image_contrast($img$percent){
    
$x imagesx($img);
    
$y imagesy($img);
    
$dest imagecreatetruecolor($x$y);
    
$percent $percent 100;
    for(
$i=0$i<$x$i++){
        for(
$j=0$j<$y$j++){
            
$rgb imagecolorat($img$i$j);
            
$r = ($rgb >> 16) & 0xFF;
            
$g = ($rgb >> 8) & 0xFF;
            
$b $rgb 0xFF;
            
$r $percent min(floor($r $percent), 255) : max(ceil($r $percent), 0);
            
$g $percent min(floor($g $percent), 255) : max(ceil($g $percent), 0);
            
$b $percent min(floor($b $percent), 255) : max(ceil($b $percent), 0);
            
imagesetpixel($dest$i$jimagecolorallocate($dest$r$g$b));
        }
    }
    return 
$dest;
}
?>

Example

image_contrast Can be used in the following way:

<?php
$img 
imagecreatefromjpeg('someimage.jpg'); // load an image

$dest image_contrast($img50);
// decrease contrast by 50%

$dest image_contrast($img150);
// increase contrast by 50%

header('Content-type: image/jpeg'); // set header

imagejpeg($dest); // send to browser
?>

Added on Jul 7th at 5 pm by Scott - 0 Comments
Updated on Aug 3rd at 4 am.


image_add_border

Information

(PHP Version >= 4.0.6)
Functions used in this snippet: imagesx, imagesy, str_replace, strlen, list, sscanf, imagecreatetruecolor, imagecolorallocate, imagefill, imagecopy.

Description

int image_add_border( int img, str color, int width )

Adds a width width pixel border to the image img in the color color.


Here is an Example of image_add_border

Snippet

<?php
function image_add_border($img$color$width=1){
    
$x imagesx($img);
    
$y imagesy($img);
    
$color str_replace('#','',$color);
    
$color strlen($color) === $color{0} . $color{0} . $color{1} . $color{1} . $color{2} . $color{2} : $color;
    list(
$r$g$b) = sscanf($color"%2x%2x%2x");
    
$dest imagecreatetruecolor(($x + ($width 2)), ($y + ($width 2)));
    
$color imagecolorallocate($dest$r$g$b);
    
imagefill($dest00$color);
    
imagecopy($dest$img$width$width00$x$y);
    return 
$dest;
}
?>

Example

image_add_border Can be used in the following way:

<?php
$img 
imagecreatefromjpeg('someimage.jpg'); // load an image

$dest image_add_border($img'000000'); 
// add a black 1 pixel border

$dest image_add_border($img'FF0000'5);
// add a red 5 pixel border

header('Content-type: image/jpeg'); // set header

imagejpeg($dest); //send to browser
?>

Added on Jul 7th at 5 pm by Scott - 0 Comments
Updated on Aug 3rd at 4 am.


« 1 2 3 4 » ... 5