Never been to DZone Snippets 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

About this user

Chris Meller http://incoherentbabble.com

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Maybe UnSerialize

Checks to see if a string is actually the serialized version of an array. If it is, we return an unserialized version. If not, return the original string. This way, we can just call our function on stored DB values and not worry about checking the results.

This was snagged from the Wordpress 2.0 source. Yayyy for Open Source!

	function maybe_unserialize ( $original ) {
		if ( false !== $gm = @ unserialize($original) )
			return $gm;
		else
			return $original;
	}

Smart MySQL Escape Function

This function first checks to see if PHP is set to automagically quote stuff. If it is, we first strip pre-quoted stuff, then (assuming our text isn't numeric), we properly quote everything.

A good bit of room for improvement here, but at the very least, you should hit this before inserting anything into your database.

	// check to see if a string needs to be escaped for database input
	function escapeit ( $text ) {
		
		if ( get_magic_quotes_gpc() ) {
			$text = stripslashes($text);
		}
		
		if ( !is_numeric($text) ) {
			
			$text = mysql_real_escape_string($text);
			
		}
		
		return $text;
		
	}
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS