Never been to CodeSnippets 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

Tracy Floyd http://www.coalescedesign.com

Convert an object to an associative array

// Converts a php object to an associative array

function object_to_array($data) 
{
  if(is_array($data) || is_object($data))
  {
    $result = array(); 
    foreach($data as $key => $value)
    { 
      $result[$key] = object_to_array($value); 
    }
    return $result;
  }
  return $data;
}

// insert code here..

Misc. terminal command reference


# Creating symlink
ln -s   [destination file/dir]    [link name]

# Import svn 
svn import -m "initial import" . http://svn.cdnm.com/repo

# Untar all .tar files in current directory:
for i in *.tar; do tar -xvzf $i; done

# Creating an archive
$ tar cjf outputfile.tar.bz2 inputs
Extract said archive to the current directory. For a compressed archive, you’ll again need to add the z for a .tar.gz, or j for .tar.bz2.
$ tar xf inputfile.tar
$ tar xjf inputfile.tar.bz2

Helpful localhosting terminal commands

// description of your code here


# Adding vhosts
mate /etc/httpd/httpd.conf
mate /etc/hosts

# Restart Apache
sudo apachectl graceful

# Recursively remove .svn folders
rm -rf `find . -type d -name .svn`

Force traffic over HTTPS

// Force traffic over HTTPS to avoid weird session dropping issues, Also handles addition or removal of www prefix as needed for your security cert,


<IfModule mod_rewrite.c>

RewriteEngine On

  # Force removal of www
  RewriteCond %{HTTP_HOST} ^www\.(.+)$
  RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

  # (Or force addition of www depending on your cert)
  RewriteEngine On
  RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 

  # Now Force traffic to use HTTPS
  RewriteCond %{SERVER_PORT} !443
  RewriteRule ^(.*)$ https://securesiteurl.com/$1 [R=301,L]

</IfModule>

Email Form Validation Related Functions

// Email form validation functions

<?php

// Function to look for suspicious looking text in submitted values
function is_injected($str) 
{
  $injections = array('(Content-Type:)','(MIME-Version:)','(Content-Transfer-Encoding:)','(From:)','(to:)','(cc:)','(bcc:)');
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str)) {
    return true;
  }
  else {
    return false;
  }
}

// Logic for page that calls the mail() function
if ($not_injected)
{
  // email send code...
}




/* Strips html tags and trims whitespace from data */
function clean_up($data) {
   $data = strip_tags($data);
   $data = trim(htmlentities($data));
   return $data;
}


?>

Return the MySQL-formatted date for 60 days from today (PHP)

// Get the date in MySQL date format for 60 days from today

$new_expiration_date = date('Y-m-d',mktime(0,0,0,date('m'),date('d')+60,date('Y')));

Turn on PHP Error Reporting

// Turn on PHP Error Reporting

ini_set("display_errors","2");
ERROR_REPORTING(E_ALL);

Toggle Block Elements

// (Used for expanding and collapsing block elements. Good for hiding divs or expanding the divs for forms.)
// Link: Read more...
// In page: <div id="more_stuff" style="display:none;">Extra content...</div>



function toggleLayer(whichLayer) 
{
	if (document.getElementById) 
	{
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display? "":"none";
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "":"none";
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "":"none";
	}
}

Print Page Form Button

// Creates a Print this Page button
// usage: document.write("<form><input type=button "+"value=\""+message+"\" onClick=\"printpage()\"></form>");
//printpage

var message = "Print this Page";
function printpage() {
window.print();  
}

Pop Up Window Generator

// Pop Up Window Generator
// (Parameters passed through URL string)
// Usage: Link Text

var win=null;

function NewWindow(mypage,myname,w,h,pos,infocus) {
	if (pos == "random") {
		myleft=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
		mytop=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
	}
	if (pos == "center") {
		myleft = (screen.width)?(screen.width-w)/2:100;
		mytop = (screen.height)?(screen.height-h)/2:100;
	} else if ((pos != 'center' && pos != "random") || pos == null) {
		myleft = 0;
		mytop = 20
	}
	settings = "width=" + w + ",height=" + h + ",top=" + mytop + ",left=" + myleft + ",scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no";
	win = window.open(mypage,myname,settings);
	win.focus();
}