Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Helpful Functions  (Read 1397 times)

nukpana

  • Hero Member
  • *****
  • Karma: 71
  • Posts: 663
Helpful Functions
« on: September 29, 2009, 01:35:42 PM »

Just a list of helpful functions that one (I) has made to help them out with coding

Code: [Select]
// Remove string from a string
function str_remove( $search, $var = '' ) {
if ( strpos( $var, $search ) === 0 ) {
$var = str_replace( $search, '', $var );
}
return $var;
}

// Usage
$url = str_remove( 'http://', $url );

Code: [Select]
// Remove null & emtpy variables from array
function array_remove( $var ) {
if ( !is_array( $var ) ) return;
foreach($var as $key => $value) {
if (!isset($value) || empty($value)) {
unset($var[$key]);
}
}
return $var;
}

// Usage
$url = explode('/', clean($_GET['category']));
// Print_r would give you say Array ( [0] => archive [1] => )
$url = array_remove( $url );
// Array ( [0] => archive)

Code: [Select]
function limitArray( $limitAmount, $array ) {
if ( !is_array($array) ) return;
foreach ( $array as $key=>$value ) {
if ( $key > $limitAmount ) {
unset( $array[$key] );
}
}
return $array;
}

// Usage
// snLite example - admin url would look like domain.com/admin/page/edit/id=1/
if ( $uri[0] == 'admin' ) {
if ( __admin ) {
// The exploded array cannot be bigger than 4 or the other parts will be discarded
$uri = limitArray( 3, $uri );
if ( isset( $uri[3] ) && strpos( $uri[3], 'id=' ) === 0 ) {
$uri['id'] = escape( substr( $uri[3], '3' ) );
}
unset( $uri[3] );
$uri = adminPageCheck( $uri );
} else {
// This is admin/login
$uri = limitArray( 1, $uri );
$uri = adminPageCheck( $uri );
}
} else {
// I don't want to over 4 levels of categorization
$uri = limitArray( 3, $uri );
}


/**********Not part of this but function from above *************/
// AdminPageCheck looks like this
function adminPages() {
$uri = array();
// Array for base admin pages
// ex domain.com/admin/page/
$uri['base'] = array( 'login' );
if ( __admin ) {
$uri['adminbase'] = array(
'logout',
'page',
'extra',
'grouping',
'plugins',
'templates',
'settings'
);
$uri['base'] = array_merge( $uri['base'], $uri['adminbase']); 
unset( $uri['adminbase'] );
}
execPlugin( 'adminURIBase', $uri['base'] );
// Array for extended admin pages
// ex domain.com/admin/page/add/
$uri['extend'] = array(
'add',
'edit',
'view',
'process'
);
execPlugin( 'extadminURIBase', $uri['extend'] );
return $uri;
}

function adminPageCheck( $uri ) {
$adminPages = adminPages();
if ( isset( $uri[2] ) ) {
if ( !in_array( $uri[2], $adminPages['extend'] ) ) {
unset( $uri[2], $uri[3], $uri['id'] );
}
}
if ( isset( $uri[1] ) ) {
if ( !in_array( $uri[1], $adminPages['base'] ) ) {
unset( $uri[1], $uri[2], $uri[3], $uri['id'] );
}
}
return $uri;
}



Code: [Select]
// Check for the file and include it
function incFile( $fileName ) {
if ( file_exists( $fileName . '.php' ) ) {
require( $fileName . '.php'  );
}
}


« Last Edit: September 29, 2009, 02:13:51 PM by nukpana »
Logged

centered

  • Guest
Re: Helpful Functions
« Reply #1 on: November 18, 2009, 01:38:26 PM »

Some more

Code: [Select]
<?php

// Initialize an array with defined keys and empty values
function intArray$arr$data ) {
if (!is_array($arr) && !is_array($data)) return;
foreach ($data as $k=>$v) {
$arr[$v] = '';
}
return $arr;
}

// Example:

// define our search fields here
// Outputs array
function fieldlist() {
return array('bktitle''artist''role''period''media''acc''recrd''ref');
}

$post = array();
if (!
$_POST) {
   
$post initArray$postfieldlist() );
} else {
   
$post $_POST;
}

echo 
'<label for="bktitle">Title: </label><input type="text" id="bktitle" name="bktitle" value="'$post['bktitle'] .'" /><br />';

?>


Code: [Select]
<?php

// Remove invalid keys from array against comparison array
function unset_invalid_key$auditedArray$definedArray ) {
if ( !is_array$auditedArray ) ) return;
foreach ($auditedArray as $k=>$v) {
if (!in_array($k$definedArray)) {
unset($k$v);
}
if (isset($k) && isset($v)) {
$arr[$k] = $v;
}
}
$arr unset_empty_value($arr);  // unset_empty_value is array_remove (first post) renamed
return $arr;
}

Example:

// Using fieldlist() from above

// $_POST = array( 'blah' =>'bad stuff', 'bktitle' => 'good stuff');
$fields unset_invalid_key$_POSTfieldlist() );
// $fields = array( 'bktitle' => 'good stuff');

?>

« Last Edit: November 18, 2009, 06:19:50 PM by equilni »
Logged

skian

  • Full Member
  • ***
  • Karma: 14
  • Posts: 120
Re: Helpful Functions
« Reply #2 on: November 18, 2009, 03:27:21 PM »

equilni

About "initArray" we can achieve the same effect (initialize attributes in array) using way fewer lines.

Please tell me what you think about this code
Code: [Select]
$attrdefaults = array('bktitle' => '', 'artist' => '', 'role' => '', 'period' => '', 'media'=> '', 'acc'=> '' ,  'recrd' => '', 'ref' => '');

$post = array('artist' => 'me');

$post = array_merge ($attrdefaults, $post);

var_dump($post);
This will produce :
Code: [Select]
array(8) {
  ["bktitle"]=>
  string(0) ""
  ["artist"]=>
  string(2) "me"
  ["role"]=>
  string(0) ""
  ["period"]=>
  string(0) ""
  ["media"]=>
  string(0) ""
  ["acc"]=>
  string(0) ""
  ["recrd"]=>
  string(0) ""
  ["ref"]=>
  string(0) ""
}

sf
« Last Edit: November 18, 2009, 09:05:41 PM by skian »
Logged