PHP string ends with
There’s a hundred ways to do this, but here’s the one I came up with. It doesn’t use regex’s so it should be pretty quick. function ends_with($str, $suffix) { return substr($str, -strlen($suffix))...
View ArticleHash/unhash HTML
Here’s a little class I wrote that lets you hash HTML and other things so that you can do some processing on just the text, and then unhash the HTML again. class ht { static $hashes = array();...
View ArticlePHP code for handling recursive categories/hierarchical data
I’m just going to paste the code I wrote here… you can read some other tutorial to understand what it’s doing, but for some reason they have really incomplete examples, so here’s the whole shabang. It...
View ArticleGet Domain & Subdomain from URL
preg_match('/^(?:www\.)?(?:(.+)\.)?(.+\..+)$/i', $_SERVER['HTTP_HOST'], $matches); define('PROTOCOL', strtolower(substr($_SERVER['SERVER_PROTOCOL'],0,strpos($_SERVER['SERVER_PROTOCOL'],'/'))).'://');...
View ArticleFacebook PHP API: Get the names of all your friends
If you didn’t already know, Facebook has an API that exposes quite a darn bit information. You can easily query this data using their API, but each request takes a fair bit of time. Typically, to get...
View ArticleSQL Injection Safe Queries Redux
function mysql_safe_string($value) { if(is_numeric($value)) return $value; elseif(empty($value)) return 'NULL'; elseif(is_string($value)) return...
View Articlearray_map_recursive
Not much needs to be said here. function array_map_recursive($callback, $arr) { $ret = array(); foreach($arr as $key => $val) { if(is_array($val)) $ret[$key] =...
View ArticleHuman Readable File Size in PHP
public static function iec_file_size($size, $digits=4, $unit=0) { $units = array('B','KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'); while($size >= 1024) { $size /= 1024;...
View ArticlePHP: Generate a unique filename for a given directory
This function will generate a filename unique to specified directory. If you’re using it to create filenames for uploaded images, for example, you can give it a path to your upload folder an extension...
View ArticleCompile all Jade files to a single client-side JavaScript file
Jade is “a high performance template engine heavily influenced by Haml and implemented with JavaScript for node”. One of it’s nice features is that it lets you compile your Jade templates into...
View Article
More Pages to Explore .....