Skip to content

Instantly share code, notes, and snippets.

View camt's full-sized avatar

Cameron camt

  • Tru-Test
  • Auckland, New Zealand
View GitHub Profile
@camt
camt / ByteToString.cs
Created January 22, 2012 08:23
algorithm for aesthetically converting bytes to strings
public static string b2s(byte b) {
string result = Convert.ToString(b, 2);
while(result.Length < 8 ) result = "0" + result;
return result;
}
@camt
camt / whitespace.js
Created January 22, 2012 08:19
trimming white space in JS easily
function trim(str, chars) {
return ltrim(rtrim(str, chars), chars);
}
function ltrim(str, chars) {
chars = chars || "\\s";
return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
function rtrim(str, chars) {
chars = chars || "\\s";
return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
@camt
camt / castToInt.js
Created January 22, 2012 08:20
casting to integers in JavaScript
val = (numberA / numberB) | 0;
@camt
camt / internalize.php
Created January 22, 2012 08:22
External redirects from your site trigger web-bots in different ways. If you want to hide them from the web-bots, and have all links on you site point internally, this script is a simple way to do so. You can either run it through a database, which makes
<?php
if(isset($_REQUEST['rd'])) {
$new = $_REQUEST['rd'];
// DB access
include("db.php");
$sql = "SELECT rdURL FROM rd WHERE rdName='".$new." LIMIT 1';";
$result = mysql_query($sql);
mysql_close($conn);
if($result) {
$row = mysql_fetch_array($result);
@camt
camt / mailto.html
Created January 24, 2012 19:14
JS creation of mailto link - avoids scrapers and bots
<script>
document.write("<a href='mailto:" + 'moc.elpmaxe@em'.split("").reverse().join("") + "'>email</a>");
</script>
@camt
camt / clientSide.php
Created January 31, 2012 00:18
Client-side time with PHP and JS
if(!$_REQUEST["client_time"])
{
$vars = "?";
foreach($_REQUEST as $key => $val) {
$vars .= $key."=".$val."&";
}
echo "<script type=\"text/javascript\">";
echo "localtime = new Date();";
echo "document.location.href = '".$PHP_SELF.$vars."client_time='
+ localtime.getTime();";
@camt
camt / distance.php
Created January 31, 2012 00:22
Calculate distance between coordinates
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) *
cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
@camt
camt / irrevocable.html
Created January 31, 2012 00:14
A simple little trick for confirming with a user before they undertake an action on your web page. When the user clicks the link, they will hit a pop up box, displaying the confirmation message and two buttons. If they click yes the link is followed and t
<a href="irrevocable-action.php?doitnow=yes" onclick="return confirm('Are you really, really, really sure you want to do this?')">Do It!!</a>
@camt
camt / RandomAlphaString.cs
Created January 31, 2012 00:15
simple random alpha char string variable in C#
private const int _length = 15;
private static string RandomAlphaString {
get {
var ret = "";
var random = new Random();
while (ret.Length < _length)
if (random.Next(1, 9) % 2 == 0)
ret = ret + Convert.ToChar(random.Next(65, 90));
else
ret = ret + Convert.ToChar(random.Next(97, 122));
@camt
camt / removeChars.php
Created January 31, 2012 00:21
Strip 'X' end characters from a string with PHP. This is faster for longer strings, as it does not need to count it's way to the end of the string first.
$string = strrev(substr(strrev($string), 2));