~ overflow ~

Archive for December, 2010

Convert Object to Array in php

by z3n on Dec.29, 2010, under Coding

Problem:
How to convert a generic object into an array?

Solution:
I’ve wrote this to convert simple xml objects into array, works pretty good.

function toArray($obj) {
	if (is_object($obj))
		$obj = (array)$obj;

	if (is_array($obj)) {
		foreach ($obj as &$val)
			$val = toArray($val);
	} else { // if this is not an array so it's a string
		$obj = (string)$obj;
	}

	return $obj;
}
Leave a Comment :, , , , more...

email validator snippet

by z3n on Dec.04, 2010, under Coding

Problem:

How to validate an email using php?

Solution:

I’m posting this function because i made it to have older php compatibility.

function email_validator($email) {
	return function_exists("filter_var") ?
			filter_var($email, FILTER_VALIDATE_EMAIL)
		: // if filter_var is not defined (php < 5.2), we will emulate php's core here
			!empty($email) && strlen($email) <= 320 &&
			/**
			 * preg info:
			 *
			 * Copyright © Michael Rushton 2009-10
			 * http://squiloople.com/
			 *
			 * more at: http://svn.php.net/viewvc/php/php-src/trunk/ext/filter/logical_filters.c?view=markup
			 */
			preg_match("/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD", $email)
	;
}
Leave a Comment :, , more...

MySQL full database search

by z3n on Dec.04, 2010, under Coding

Problem:

You need to find a entry or a specific value that’s into a table from a database, but you don’t know what is the table neither the database for sure.

Solution:

Use my happy mysql full database search script.
It’s so fun when all you gotta do is type a single command.

download mysql full database search script

Leave a Comment :, , , more...

SVN folder cleaning (svn wipe)

by z3n on Dec.04, 2010, under Coding

Problem:

You did a svn checkout on this nice project but now you don’t want that .svn folders all over the place, after all you won’t be commiting it. Suddenly you find out that there’s thousands of folders with thousands of files.

Solution:

I wrote this little php cli script to fix this:

<?php

// (c) z3n - R1V1@101203 - www.overflow.biz - rodrigo.orph@gmail.com

function _o($msg) {
	echo "\r" . substr($msg,0, 79) . str_repeat(" ", 79 - strlen($msg));
}

function rddir($dir) { /* v2.17 */
	global $tfile,$tdir;

	if (is_dir($dir)) {
		if ($dh = opendir($dir)) {
			while (($file = readdir($dh)) !== false) {
				if ($file != "." && $file != "..") {
					if (is_dir($dir . $file))
						$tdir[] = $dir . $file;
					else
						$tfile[] = $file;
				}
			}
			closedir($dh);
		}
	}
}

if (!isset($argv[1]))
	die("Usage: " . $_SERVER['PHP_SELF'] . " <path/to/svn_root/>");

// browse dirs searching for .svn

for (
	$tdir = array($argv[1]),
	$tfile = array(),
	$svn = array(),
	$i = 0;$i < count($tdir);$i++)
{
	rddir($tdir[$i]);
	_o("Reading: ".$tdir[$i]." ...");

	foreach ($tfile as $entry) {
		if ($entry != "." && $entry != "..") {
			$fn = $tdir[$i] . (PHP_OS == "WINNT" ? "\\" : "/") . $entry;
			if ($entry == ".svn" && !in_array($fn, $svn))
				$svn[] = $fn;

			if (is_dir($fn) && $entry != ".svn")
				$tdir[] = $fn;
		}
	}

	$tfile = array();
}

_o("Found: " . count($svn) . " .svn folders, collecting files...");

$files = array();
$dirs = array();
$t = 0;

// collect files from .svn folders

foreach ($svn as $dir) {
	for ($tdir = array($dir), $tfile = array(),$i = 0;$i < count($tdir);$i++) {
		rddir($tdir[$i]);
		_o("[" . $t . "] - Collecting: ". $tdir[$i] ."...");

		foreach ($tfile as $entry) {
			if ($entry != "." && $entry != "..") {
				$fn = $tdir[$i] . (PHP_OS == "WINNT" ? "\\" : "/") . $entry;
				$t++;

				if (is_dir($fn)) {
					$tdir[] = $fn;
					$dirs[] = $fn;
				} else {
					$files[] = $fn;
				}
			}
		}
	}
}

$i = count($files) + count($dirs) + count($svn);

// delete files

foreach ($files as $file) {
	_o("[" . $i . "] Deleting Files: " . $file);
	unlink($file);
	$i--;
}

// delete subfolders

rsort($dirs);

foreach ($dirs as $dir) {
	_o("[" . $i . "] Deleting Dir: " . $dir);
	rmdir($dir);
	$i--;
}

// delete root .svn folders

foreach ($svn as $dir) {
	_o("[" . $i . "] Deleting Roots: " . $dir);
	rmdir($dir);
	$i--;
}

echo "\nDone!";

?>
Leave a Comment :, , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!