Skip to content

Instantly share code, notes, and snippets.

@joshhartman
joshhartman / gist:846721
Created February 28, 2011 00:08
Get File Extension PHP Benchmark
<?php
$file = 'c:\\xampplite\\htdocs\\index.php';
$max = 1000000;
?>
<p>Command: <strong>$ext = end(explode('.', $file));</strong></p>
<?php
// Start TIMER
$stimer = explode( ' ', microtime() );
$stimer = $stimer[1] + $stimer[0];
?>
@joshhartman
joshhartman / gist:878687
Created March 20, 2011 21:16
Select Multiple Random Rows with MySQL PHP Benchmark
<?php
$max = 100;
$table = 'mytable';
$limit = $max;
$mysql = mysql_connect('localhost', 'root', '');
mysql_query('USE test');
?>
<h2>Order By Rand()</h2>
<?php
// Start TIMER
@joshhartman
joshhartman / gist:914796
Created April 12, 2011 02:17
Convert a PHP Array to an Object
<?php
function arrayToObject($array){
return is_array($array) ? (object)array_map('arrayToObject', $array) : $array;
}
// Start with a multi-dimensional associative array
$array = array('software' => array(
'apache' => array('version' => '2.2.14', 'website' => 'http://www.apache.org'),
'php' => array('version' => '5.3.1', 'website' => 'http://www.php.net'),
@joshhartman
joshhartman / gist:962554
Created May 9, 2011 13:55
Calculate the Distance Between Two Geo Coordinates
<?php
// Calculate distance between 2 sets of longitude/latitude coordinates
// Takes two sets of coordinates in decimal longitude and latitude format and returns the distance in miles.
// php, distance, coordinates, longitude, latitude
// Oelwein, IA
$lat1 = 42.6733171;
$lon1 = -91.9135036;
// Independence, IA
@joshhartman
joshhartman / gist:995148
Created May 27, 2011 12:30
Random Alphanumeric String Generator
<?php
function random_string($length, $max = false)
{
if (is_int($max) && $max > $length) {
$length = mt_rand($length, $max);
}
$output = '';
for ($i = 0; $i < $length; $i++) {
$which = mt_rand(0, 2);
@joshhartman
joshhartman / mcrypt-ecb.php
Last active May 16, 2022 15:08
Rijndael 256-bit Encryption Function (ECB)
<?php
// Define an strong encryption key to use with MCRYPT
// Note: The same encryption key used to encrypt the data must be used to decrypt the data correctly
define('ENCRYPTION_KEY', 'ayf9y0fuY3WN27bha45c65g5pw8FEep7');
// Encrypt Function
function mc_encrypt($encrypt, $mc_key){
$encrypt = serialize($encrypt);
$passcrypt = trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($encrypt),
MCRYPT_MODE_ECB));
@joshhartman
joshhartman / MY_Router.php
Created July 5, 2011 21:59
Modify CodeIgniter to Support Hyphens in URL Request Segment
<?php
/*
|
| Put this file (MY_Router.php) inside /application/core (CI2)
| Remember that this will effect all segments, not only module, controller and method.
|
| Alternative to this extend is to add each segment to application/config/routes.php
| $route['this-is-a-module-or-controler'] = 'this_is_a_module_or_controller';
|
| As you can see the extend method would be easier to use. You can choose to make
@joshhartman
joshhartman / randomPassword.php
Last active January 8, 2024 13:29
Human Readable Password Generator (Requires PHP 7.1+)
<?php
function randomPassword( $len = 10, $ucfirst = true, $spchar = true ){
/* Programmed by Christian Haensel
* christian@chftp.com
* http://www.chftp.com
*
* Exclusively published on weberdev.com.
* If you like my scripts, please let me know or link to me.
* You may copy, redistribute, change and alter my scripts as
* long as this information remains intact.
@joshhartman
joshhartman / gist:2044600
Created March 15, 2012 14:53 — forked from luetkemj/wp-query-ref.php
WP: Query $args
<?php
/**
* WordPress Query Comprehensive Reference
* Compiled by luetkemj - luetkemj.com
*
* CODEX: http://codex.wordpress.org/Class_Reference/WP_Query
* Source: http://core.trac.wordpress.org/browser/tags/3.3.1/wp-includes/query.php
*/
$args = array(
@joshhartman
joshhartman / linkify.php
Created March 27, 2012 03:06 — forked from jasny/linkify.php
PHP function to turn all URLs in clickable links
<?php
/**
* Turn all URLs in clickable links.
*
* @param string $value
* @param array $protocols http/https, ftp, mail, twitter
* @param array $attributes
* @param string $mode normal or all
* @return string
*/