Skip to content

Instantly share code, notes, and snippets.

@LaffinToo
LaffinToo / CutText.php
Created December 8, 2011 08:48
Trims Text for display
// Cut Text
// Cuts a string down and adds a ... signifier
// @Param1 - Text
// @Param2 - Optional Length, default 40 characters
// Return - Text Cut down to length
function CutText($var,$len=40)
{
return strlen($var)>$len?substr($var,0,($len-3)):$var;
}
@LaffinToo
LaffinToo / linkify.php
Created December 8, 2011 08:52
Creates links from plain text links (http/https/ftp/mailto/telnet)
function linkify($matches)
{
return '<a class="link" href="'. $matches[1] .'" target="_blank">'. $matches[4] .'</a>';
}
$pr = preg_replace_callback('@((https?|ftp|mailto|telnet):(//)?([^\s]{4,}))@i','linkify',$line);
@LaffinToo
LaffinToo / bytesize.php
Created December 8, 2011 08:56
Returns units based on bytes
function ByteSize($size)
{
static $unit = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$size/=(pow(1024,($idx=floor(log($size)/log(1024)))));
return number_format($size,2).$unit[$idx];
}
@LaffinToo
LaffinToo / bittwiddle.php
Created December 31, 2011 07:23
Bit Twiddling
<?php
function setbit($flags,$position)
{
return $flags|(1<<$position);
}
function resetbit($flags,$position)
{
return $flags&(~(1<<$position));
}
function togglebit($flags,$position)
@LaffinToo
LaffinToo / bracket.php
Created December 31, 2011 07:24
Tourney Bracket System
<?php
$teams=array('Alpha','Beta','Gamma','Delta','Epsilon','Zeta','Eta');
define('MY_EOL','<br .>'.PHP_EOL);
$round=0;
$participants=$teams;
while(count($participants)>1)
{
$round++; // Increment our round
@LaffinToo
LaffinToo / twittify.php
Created December 31, 2011 07:25
Twitify Links - converts twitter type text to links
<?php
// Parse Twitter nicks/lists to urls
// Internal routine
// This has a couple of functions
// TwittifyLinks() - returns True if template is set, otherwise false
// TwittifyLinks($arr,true) - $arr is a 2 element array, each holding a template for usernames/groups
// TwittifyLinks($twit) - $twit is the user/group to convert to a link
function TwittifyLinks($var=null,$set=false)
{
@LaffinToo
LaffinToo / weights.php
Created December 31, 2011 07:26
Weighted Probability - A simple weighted probability system
<?php
// Weight percentage system
// By: Laffin
// Gives weights to items, and randomly chooses items based on weight
$items = array(
array('lint',40),
array('copper coin',35),
array('silver coin',15),
array('gold coin',5),
@LaffinToo
LaffinToo / breadcrumbs.php
Created December 31, 2011 08:05
Simple Breadcrumb system - dynamicly built, Use with Cache
<?php
if(!$db=sqlite_open(':memory:'))
die('DB Open failure');
sqlite_exec('CREATE TABLE category(id INTEGER PRIMARY KEY, Name TEXT, Parent Integer)',$db);
$data =array(
'Family/Events/Birthdays',
'Family/Events/Holidays/Halloween',
@LaffinToo
LaffinToo / paginate.php
Created December 31, 2011 18:44
Pagination calculation routine #paginate #pagination
<?php
/* Wrote this for a pagination routine awhile back
the variables needed
$range = Amount of numbers to display
$curpage = Current number
$pages = Highest Page count
*/
$midrange=intval(floor($range/2));
$range_s=(($curpage-$midrange)<1?1:(($curpage+$midrange)>$pages?($pages-$range+1):$curpage-$midrange));
@LaffinToo
LaffinToo / Bitman.php
Created May 3, 2014 04:05
Bit Manipulation Class - Using strings as a bitstring allowing for huge amount of toggles #Bits
<?php
/*
* Bitman.php
*
* Copyright 2014 Luis "Laffin" Espinoza <laffintoo@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.