PHP Snippets
« 1 2 3 4 » ... 5piglatin
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
?>
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!!
?>
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, $i, 3));
}
$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], 1, 2);
}
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
?>
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 > 1 ? min(floor($r * $percent), 255) : max(ceil($r * $percent), 0);
$g = $percent > 1 ? min(floor($g * $percent), 255) : max(ceil($g * $percent), 0);
$b = $percent > 1 ? min(floor($b * $percent), 255) : max(ceil($b * $percent), 0);
imagesetpixel($dest, $i, $j, imagecolorallocate($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($img, 50);
// decrease contrast by 50%
$dest = image_contrast($img, 150);
// increase contrast by 50%
header('Content-type: image/jpeg'); // set header
imagejpeg($dest); // send to browser
?>
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) === 3 ? $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($dest, 0, 0, $color);
imagecopy($dest, $img, $width, $width, 0, 0, $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
?>
« 1 2 3 4 » ... 5