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

Sean Murphy http://IamSeanMurphy.com

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

Javascript Binary Search

Search an array using the binary search algorithm.

   1  
   2  searchArray = function(needle, haystack, case_insensitive) {
   3  	if (typeof(haystack) === 'undefined' || !haystack.length) return -1;
   4  	
   5  	var high = haystack.length - 1;
   6  	var low = 0;
   7  	case_insensitive = (typeof(case_insensitive) === 'undefined' || case_insensitive) ? true:false;
   8  	needle = (case_insensitive) ? needle.toLowerCase():needle;
   9  	
  10  	while (low <= high) {
  11  		mid = parseInt((low + high) / 2)
  12  		element = (case_insensitive) ? haystack[mid].toLowerCase():haystack[mid];
  13  		if (element > needle) {
  14  			high = mid - 1;
  15  		} else if (element < needle) {
  16  			low = mid + 1;
  17  		} else {
  18  			return mid;
  19  		}
  20  	}
  21  	
  22  	return -1;
  23  };

time_since() - Display elapsed time in a nice format.

Display elapsed time in a nice format.

   1  
   2  function time_since($older_date, $newer_date = false) {
   3  	
   4      $chunks = array(
   5                    array(60 * 60 * 24 * 365 , 'year'),
   6                    array(60 * 60 * 24 * 30 , 'month'),
   7                    array(60 * 60 * 24 * 7, 'week'),
   8                    array(60 * 60 * 24 , 'day'),
   9                    array(60 * 60 , 'hour'),
  10                    array(60 , 'minute'),
  11                );
  12  
  13      $newer_date = ($newer_date == false) ? (time()) : $newer_date;
  14  
  15      // difference in seconds
  16      $since = $newer_date - $older_date;
  17  
  18      // we only want to output two chunks of time here, eg:
  19      // x years, xx months
  20      // x days, xx hours
  21      // so there are only two bits of calculation below:
  22  
  23      // step one: the first chunk
  24      for ($i = 0, $j = count($chunks); $i < $j; $i++) {
  25          $seconds = $chunks[$i][0];
  26          $name = $chunks[$i][1];
  27  
  28          // finding the biggest chunk (if the chunk fits, break)
  29          if (($count = floor($since / $seconds)) != 0) {
  30              break;
  31          }
  32      }
  33  
  34      $output = ($count == 1) ? '1 '.$name : "$count {$name}s";
  35  
  36      // step two: the second chunk
  37      if ($i + 1 < $j) {
  38          $seconds2 = $chunks[$i + 1][0];
  39          $name2 = $chunks[$i + 1][1];
  40  
  41          if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) {
  42              // add to output var
  43              $output .= ($count2 == 1) ? ', 1 '.$name2 : ", $count2 {$name2}s";
  44          }
  45      }
  46  
  47      return $output;
  48  }

Parsing Errors on Command Line

Get PHP parsing errors on command line. Useful for those extreme cases where you can't get them to print to the browser.

   1  
   2  find . -name \*.php \! -exec php -l {} \;

Find and replace

This will search all files recursively for SEARCH_STRING and replace all occurrences of SEARCH_STRING with REPLACE_STRING throughout each unique file found. It also creates a backup of each modified file so that FILE is backed-up as FILE~ (with a tilde).

   1  
   2  grep -R --files-with-matches 'SEARCH_STRING' . | sort | uniq | xargs perl -pi~ -e 's/SEARCH_STRING/REPLACE_STRING/'
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS