PHP Snippets
« 1 2 3 »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
?>
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
?>
parse_links
Information
(PHP Version >= 3.0.9)Functions used in this snippet: ceil, preg_match_all, strlen, substr, preg_match, str_replace, preg_replace.
Description
str parse_links( str str[, int len[, str mid]] )
Adds around any links inside of a area of text. It works with [url], [url=site], and just plain http://site.com.
Snippet
<?php
function parse_links($str, $len=25, $mid='...'){
$left = ceil(0.6666 * $len);
$right = $len - $left;
preg_match_all('/(?<!=|\]|\/)((https?|ftps?|irc):\/\/|' . '(www([0-9]{1,3})?|ftp)\.)([0-9a-z-]{1,25}' . '[0-9a-z]{1}\.)([^\s&\[\{\}\]]+)/ims', $str, $matches);
foreach($matches[0] as $key=>$value){
$temp = $value;
if(strlen($value) > ($len + strlen($mid) + 2)){
$value = substr($value, 0, $left) . $mid . substr($value,(-1 * $right));
}
$temp = !preg_match('/:\/\//', $temp) ? (substr($temp, 0, 3) === 'ftp' ? 'ftp://' . $temp : 'http://' . $temp) : $temp;
$temp = $temp === $matches[0][$key] && $value === $matches[0][$key] ? '' : '=' . $temp;
$str = str_replace($matches[0][$key],'[url' . $temp . ']' . $value . '[/url]', $str);
}
$str = preg_replace('/\[url=(?!http|ftp|irc)/ims', '[url=http://', $str);
$str = preg_replace('/\[url\](.+?)\[\/url\]/ims','<a href="$1" title="$1">$1</a>',$str);
$str = preg_replace('/\[url=(.+?)\](.+?)\[\/url\]/ims', '<a href="$1" title="$1">$2</a>', $str);
return $str;
}
?>
Example
parse_links Can be used in the following way:
<?php
echo parse_links('http://www.site.com');
// <a href="http://www.site.com">http://www.site.com</a>
echo parse_links('www.cropp.in');
// <a href="http://www.cropp.in">www.cropp.in</a>
echo parse_links('ftp://somesite.com');
// <a href="ftp://somesite.com">ftp://somesite.com</a>
echo parse_links('[url]www.cropp.in[/url]');
// <a href="http://www.cropp.in">www.cropp.in</a>
echo parse_links('[url=www.cropp.in]Cropp.in[/url]');
// <a href="http://www.cropp.in">Cropp.in</a>
echo parse_links( 'http://www.somesite.com/with/a/really/long/url/link' );
// <a href="http://www.somesite.com/with/a/really/long/url/link"> http://www.some...url/link</a>
?>
make_page_links
Information
(PHP Version >= 3)Functions used in this snippet: intval, ceil, min, max.
Description
str make_page_links( int page, int num_pages, str link [, int find [, int offset ]])
make_page_links will make numbered links for multiple pages of data. The function uses the current page of the pages that are available. The pages are split up by how many total entries there are (num_pages) and how many entries per page should be shown (find defaults to 10).
offset is the offset of numbering to show for the links (optional).
Snippet
<?php
function make_page_links($page,$num_pages,$link,$find=10,$offset=3){
$num_pages = intval(ceil($num_pages / $find));
if($num_pages == 1){
return '';
}
if($page < 1){
$page = 1;
}
if($page > $num_pages){
$page = $num_pages;
}
$ret = '';
if($page > $offset){
$ret = '<a href="' . $link . '">1</a> ... ';
}
if($page > 1){
$ret .= '<a href="' . $link . ($page - 1) . '/">«</a> ';
}
$max = min($page + ($offset - 1), $num_pages);
for($i = max(1, $page - ($offset - 1)); $i <= $max; $i++){
$ret .= ($i == $page ? '<strong>' . $i . '</strong>' : '<a href="' . $link . ($i != 1 ? $i . '/' : '') . '">' . $i . '</a> ');
}
if($page < ($num_pages)){
$ret .= '<a href="' . $link . ($page + 1) . '/">»</a> ';
}
if($page < ($num_pages - $offset + 1)){
$ret .= '... <a href="' . $link . $num_pages . '/">' . $num_pages . '</a>';
}
return $ret;
}
?>
Example
make_page_links Can be used in the following way:
<?php
// be on page 3 of 300 entries with a link to index.php
echo make_page_links('3', 300, '/index.php?page=');
// <12345> ... 30
echo make_page_links(7, 300, '/index.php?page=');
// 1 ... <56789> ... 30
echo make_page_links(1, 300, '/index.php?page=', 30, 5);
// 12345> ... 10
?>
« 1 2 3 »