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

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

Apache htaccess authentication

For setting up a quick protected directory, put this in .htaccess

AuthName "Some Admin Area"
AuthType Basic
AuthUserFile /home/someclient/public_html/admin/.htpasswd
require valid-user


And then create the .htpasswd file via
htpasswd -c .htpasswd someuser

PHP clean string for mysql

// Function to clean up a string before using it in a mysql query

function clean_for_mysql($string,$max_length) {        
  $in_string = ltrim($string);       
  $in_string = rtrim($in_string);
  if (round($max_length) < 1) {  
    $max_length = 131072; // 128K
  }
  if (strlen($in_string) > $max_length) {
    $new_string = substr($in_string,0,$max_length);
  }
  $new_string = mysql_real_escape_string($new_string);
  return $new_string;
}

PHP filename bad character filter

// Function to filter out bad characters in a given filename

function replace_bad_filename_chars($filename) {
  $filtered_filename = "";

  $patterns = array(
    "/\s/", # Whitespace
    "/\&/", # Ampersand
    "/\+/"  # Plus
  );
  $replacements = array(
    "_",   # Whitespace
    "and", # Ampersand
    "plus" # Plus
  );
  
  $filename = preg_replace($patterns,$replacements,$filename);
  for ($i=0;$i<strlen($filename);$i++) {
    $current_char = substr($filename,$i,1);
    if (ctype_alnum($current_char) == TRUE || $current_char == "_" || $current_char == ".") {
      $filtered_filename .= $current_char;
    }
  }     
        
  return $filtered_filename;
}

PHP dollar format

// Function to return amount in the format $#.##

function dollar_format($amount) {
  $new_amount = "\$".sprintf("%.2f",$amount);
  return $new_amount;
}

PHP email validation function

// Simple function to check if an given email adddress is valid

function is_valid_email($email) {
  $result = TRUE;
  if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
    $result = FALSE;
  }
  return $result;
}
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS