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-7 of 7 total  RSS 

Sample date calculations

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

Get file extension

   1  
   2  function file_extension($filename)
   3  {
   4      $path_info = pathinfo($filename);
   5      return $path_info['extension'];
   6  }

Emulate register_globals off

   1  
   2  function unregister_GLOBALS()
   3  {
   4  if (!ini_get('register_globals')) {
   5         return;
   6     }
   7  
   8     // Might want to change this perhaps to a nicer error
   9     if (isset($_REQUEST['GLOBALS']) || isset($_FILES['GLOBALS'])) {
  10         die('GLOBALS overwrite attempt detected');
  11     }
  12  
  13     // Variables that shouldn't be unset
  14     $noUnset = array('GLOBALS',  '_GET',
  15                       '_POST',    '_COOKIE',
  16                       '_REQUEST', '_SERVER',
  17                       '_ENV',    '_FILES');
  18  
  19     $input = array_merge($_GET,    $_POST,
  20                           $_COOKIE, $_SERVER,
  21                           $_ENV,    $_FILES,
  22                           isset($_SESSION) && is_array($_SESSION) ? $_SESSION : array());
  23    
  24     foreach ($input as $k => $v) {
  25         if (!in_array($k, $noUnset) && isset($GLOBALS[$k])) {
  26             unset($GLOBALS[$k]);
  27         }
  28     }
  29  }
  30  
  31  unregister_GLOBALS();


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

Convert string to underscore_name

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

   1  
   2  function string_to_underscore_name($string)
   3  {
   4      $string = preg_replace('/[\'"]/', '', $string);
   5      $string = preg_replace('/[^a-zA-Z0-9]+/', '_', $string);
   6      $string = trim($string, '_');
   7      $string = strtolower($string);
   8      
   9      return $string;
  10  }

Check whether string begins with given string

Checks whether $string begins with $search

   1  
   2  function string_begins_with($string, $search)
   3  {
   4      return (strncmp($string, $search, strlen($search)) == 0);
   5  }

string_ends_with

   1  
   2  function string_ends_with($string, $ending)
   3  {
   4      $len = strlen($ending);
   5      $string_end = substr($string, strlen($string) - $len);
   6     
   7      return $string_end == $ending;
   8  }

Pretty urls

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

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