Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

Nick Trew

« Newer Snippets
Older Snippets »
3 total  XML / RSS feed 

Character Entities

// Encode a string into character entities (useful for anti-spam email links).

<?php
function encode_str ($input)
{
    for ($i = 0; $i < strlen($input); $i++) {
         $output .= "&#".ord($input[$i]).';';
    }
    $output = htmlspecialchars($output);
    return $output;
}
?>

String Hashing

// Create a strong string hash - suitable for use as a password.

<?php
function hash ($input)
{
    $salt   = 'SOME_RANDOM_STRING_HERE';
    $result = sha1(md5($input.$salt).$salt);
    return $result;
}
?>

Random String Generator

// Generate a random-length string

<?php
// Pass $size as a KB value - see the PHP filesize() function.
function fsformat ($size)
{
    if ($size < 1024)
    {
        return $size.'KB';
    }
    else if ($size >= 1024 && $size < 1048576)
    {
        return round(($size/1024), 0).'MB';
    }
    else if ($size >= 1048576 && $size < 1073741824)
    {
        return round(($size/1048576), 0).'GB';
    }
    else if ($size >= 1073741824 && $size < 1099511627776)
    {
        return round(($size/1073741824), 0).'TB';
    }
    else
    {
        return 'Error';
    }
}
?>
« Newer Snippets
Older Snippets »
3 total  XML / RSS feed