Skip to content

Instantly share code, notes, and snippets.

@johnsardine
Created October 21, 2012 05:31
Show Gist options
  • Save johnsardine/3926005 to your computer and use it in GitHub Desktop.
Save johnsardine/3926005 to your computer and use it in GitHub Desktop.
PHP Helpers
/**
* Return Passed variable if exists
*
* Allows to return a variable but only if it exists
*
* @access public
* @param var &$key
* @param string $default (default: null)
* @return void
*/
function data(&$key, $default = null)
{
return (isset($key)) ? $key : $default ;
}
/**
* Looks in an url for a query parameter and replaces it or creates if it does not exist
*
* @access public
* @param mixed $key
* @param mixed $value
* @param mixed $url
* @return void
*/
function push_param($key, $value, $url)
{
if (strpos($url, $key.'=')) {
return preg_replace('/'.$key.'(\=[^&]*)/', $key.'='.$value, $url);
} else {
if (strpos($url, '?')) {
return $url . '&'.$key.'='.$value;
} else {
return $url . '?'.$key.'='.$value;
}
}
}
/**
* Return GET parameter
*
* Easier access to $_GET value
*
* Returns value if exists, returns false if it does not
*
* @access public
* @param mixed $key (default: null)
* @return void
*/
function get($key = null, $default = null)
{
if (!$key && !empty($_GET)) {
return $_GET;
} elseif (!$key) {
return false;
}
return (isset($_GET[$key])) ? $_GET[$key] : $default ;
}
/**
* Return POST parameter
*
* Easier access to $_GET value
*
* Returns value if exists, returns false if it does not
*
* @access public
* @param mixed $key (default: null)
* @return void
*/
function post($key = null)
{
if (!$key && !empty($_POST)) {
return $_POST;
} elseif (!$key) {
return false;
}
$args = func_get_args();
$str = '$_POST';
foreach ($args as $index => $key) {
$str .= '["'.$key.'"]';
}
$str .= '';
return eval('return (isset('.$str.')) ? '.$str.' : null ;');
}
function file_ext($filename)
{
if ( !preg_match('/\./', $filename) ) return '';
return preg_replace('/^.*\./', '', $filename);
}
function file_ext_strip($filename)
{
return preg_replace('/\.[^.]*$/', '', $filename);
}
/**
* Replaces hyphen with underscore
*
* Replaces all hyphens in a string with undersocres,
* useful to convert url parameters to class/method friendly names
*
* @access public
* @param mixed $string (default: null)
* @return void
*/
function hyphen_to_underscore($string = null)
{
if (!$string) return false ;
return str_replace('-', '_', $string);
}
/**
* Merge user defined arguments into defaults array.
*
* This function is used to allow for both string or array
* to be merged into another array.
*
* @param string|array $args Value to merge with $defaults
* @param array $defaults Array that serves as the defaults.
* @return array Merged user defined values with defaults.
*/
function parse_args( $args, $defaults = '' )
{
if ( is_object( $args ) )
$r = get_object_vars( $args );
elseif ( is_array( $args ) )
$r =& $args;
else
parse_str( $args, $r );
if ( is_array( $defaults ) )
return array_merge( $defaults, $r );
return $r;
}
function real_escape_string($input)
{
if (is_array($input))
return array_map(__METHOD__, $input);
// "\n", "\r"
if (!empty($input) && is_string($input)) {
return str_replace(array('\\', "\0", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $input);
}
return $input;
}
function array_to_object($d)
{
if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return (object) array_map(__FUNCTION__, $d);
}
else {
// Return object
return $d;
}
}
function object_to_array($d)
{
if (is_object($d)) {
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
}
if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return array_map(__FUNCTION__, $d);
}
else {
// Return array
return $d;
}
}
function array_search_recursive($haystack, $needle, $index = null)
{
$aIt = new RecursiveArrayIterator($haystack);
$it= new RecursiveIteratorIterator($aIt);
while ($it->valid()) {
if (((isset($index) and ($it->key() == $index)) or (!isset($index))) and ($it->current() == $needle)) {
return $aIt->key();
}
$it->next();
}
return false;
}
function return_hierarchical($array, $options = array('parent_key' => 'item_parent', 'children_key' => 'children'))
{
$p = array();
foreach ($array as $row) {
$parent_id = $row[$options['parent_key']];
$id = $row['id'];
if (!isset($p[$parent_id]))
$p[$parent_id] = array($options['children_key'] => array());
if (isset($p[$id]))
$child = &$p[$id][$options['children_key']];
else
$child = array();
$p[$id] = $row;
$p[$id][$options['children_key']] = &$child;
unset($p[$id]['parent_id']);
unset($child);
$p[$parent_id][$options['children_key']][] = &$p[$id];
}
$p = array_reverse($p);
foreach ($p as $p) {
if (count($p) == 1) {
$hierarchy = $p;
break;
}
}
return $hierarchy[$options['children_key']];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment