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

Algimantas Stancelis

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

Sample date calculations

  Today: <?php echo date('Y-m-d') ?> <br />
  Tomorrow: <?php echo date('Y-m-d', strtotime('+1 day')) ?> <br />
  1 week later: <?php echo date('Y-m-d', strtotime('+1 week')) ?> <br />
  1 month later: <?php echo date('Y-m-d', strtotime('+1 month')) ?> <br />

Get file extension

function file_extension($filename)
{
    $path_info = pathinfo($filename);
    return $path_info['extension'];
}

Emulate register_globals off

function unregister_GLOBALS()
{
if (!ini_get('register_globals')) {
       return;
   }

   // Might want to change this perhaps to a nicer error
   if (isset($_REQUEST['GLOBALS']) || isset($_FILES['GLOBALS'])) {
       die('GLOBALS overwrite attempt detected');
   }

   // Variables that shouldn't be unset
   $noUnset = array('GLOBALS',  '_GET',
                     '_POST',    '_COOKIE',
                     '_REQUEST', '_SERVER',
                     '_ENV',    '_FILES');

   $input = array_merge($_GET,    $_POST,
                         $_COOKIE, $_SERVER,
                         $_ENV,    $_FILES,
                         isset($_SESSION) && is_array($_SESSION) ? $_SESSION : array());
  
   foreach ($input as $k => $v) {
       if (!in_array($k, $noUnset) && isset($GLOBALS[$k])) {
           unset($GLOBALS[$k]);
       }
   }
}

unregister_GLOBALS();


Source: http://www.zend.com/manual/faq.misc.php#faq.misc.registerglobals

Check whether table exists

Checks whether mysql table exists.

SHOW TABLES LIKE 'table_name'

Convert string to underscore_name

Converts "My House" to "my_house".
Converts " Peter's nice car " to "peters_nice_car".
Converts "_88" to "88"

function string_to_underscore_name($string)
{
    $string = preg_replace('/[\'"]/', '', $string);
    $string = preg_replace('/[^a-zA-Z0-9]+/', '_', $string);
    $string = trim($string, '_');
    $string = strtolower($string);
    
    return $string;
}

Check whether string begins with given string

Checks whether $string begins with $search

function string_begins_with($string, $search)
{
    return (strncmp($string, $search, strlen($search)) == 0);
}

string_ends_with

function string_ends_with($string, $ending)
{
    $len = strlen($ending);
    $string_end = substr($string, strlen($string) - $len);
   
    return $string_end == $ending;
}

Pretty urls

If url is 'index.php/hello' then $request will be 'hello'

$request = $_SERVER['PATH_INFO'];
if (isset($request[0]) && ($request[0] == '/')) {
    $request = substr($request, 1);
}
« Newer Snippets
Older Snippets »
Showing 1-8 of 8 total  RSS