Never been to TextSnippets 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 (or not, you can keep them private!)

About this user

Tracy Floyd www.cdnm.com

« Newer Snippets
Older Snippets »
32 total  XML / RSS feed 

Force traffic to use SSL (HTTPS://)

// Forces incoming traffic to use SSL connection

 #Force SSL
 RewriteCond %{SERVER_PORT} !443
 RewriteRule ^(.*)$ https://securesiteurl.com/$1 [R=301,L]

is_valid_email_address function

// description of your code here


/* Regex for validating email address */
function is_valid_email_address($email_address)
{
  if (ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$",$email_address) )
  {
    return TRUE;
  }
  else
  {
    return FALSE;
  }
}

Email Form Validation Related Functions

// Email form validation functions

<?php

// Function to look for suspicious looking text in submitted values
function is_injected($str) 
{
  $injections = array('(Content-Type:)','(MIME-Version:)','(Content-Transfer-Encoding:)','(From:)','(to:)','(cc:)','(bcc:)');
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str)) {
    return true;
  }
  else {
    return false;
  }
}

// Logic for page that calls the mail() function
if ($not_injected)
{
  // email send code...
}




/* Strips html tags and trims whitespace from data */
function clean_up($data) {
   $data = strip_tags($data);
   $data = trim(htmlentities($data));
   return $data;
}


?>

Return the MySQL-formatted date for 60 days from today (PHP)

// Get the date in MySQL date format for 60 days from today

$new_expiration_date = date('Y-m-d',mktime(0,0,0,date('m'),date('d')+60,date('Y')));

Turn on PHP Error Reporting

// Turn on PHP Error Reporting

ini_set("display_errors","2");
ERROR_REPORTING(E_ALL);

Toggle Block Elements

// (Used for expanding and collapsing block elements. Good for hiding divs or expanding the divs for forms.)
// Link: Read more...
// In page:



function toggleLayer(whichLayer) 
{
        if (document.getElementById) 
        {
                // this is the way the standards work
                var style2 = document.getElementById(whichLayer).style;
                style2.display = style2.display? "":"none";
        }
        else if (document.all)
        {
                // this is the way old msie versions work
                var style2 = document.all[whichLayer].style;
                style2.display = style2.display? "":"none";
        }
        else if (document.layers)
        {
                // this is the way nn4 works
                var style2 = document.layers[whichLayer].style;
                style2.display = style2.display? "":"none";
        }
}

Print Page Form Button

// Creates a Print this Page button
// usage: document.write("
");
//printpage

var message = "Print this Page";
function printpage() {
window.print();  
}

Pop Up Window Generator

// Pop Up Window Generator
// (Parameters passed through URL string)
// Usage: Link Text

var win=null;

function NewWindow(mypage,myname,w,h,pos,infocus) {
        if (pos == "random") {
                myleft=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
                mytop=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
        }
        if (pos == "center") {
                myleft = (screen.width)?(screen.width-w)/2:100;
                mytop = (screen.height)?(screen.height-h)/2:100;
        } else if ((pos != 'center' && pos != "random") || pos == null) {
                myleft = 0;
                mytop = 20
        }
        settings = "width=" + w + ",height=" + h + ",top=" + mytop + ",left=" + myleft + ",scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no";
        win = window.open(mypage,myname,settings);
        win.focus();
}

Text Cleaner

// This function will remove unwanted stuff from submitted text.
// usage: $var = text_cleaner($str);


function text_cleaner($text){
        $text=str_replace("\"",""",$text); // Get rid of curly quotation marks
        $text=str_replace("  "," ",$text); // Get rid of double spaces (not tabs)
        $text=str_replace("\r","

",$text); //Windows files do \r\n, this is for a file created in Windows }

Character Chopper

// This function will limit by number of characters.
// usage: $var = char_chop($str(string), $string_length limit of chars, default: 30));


function char_chop($str, $string_length = 30) {
        $s = strlen($str);
        if($s > $string_length){
                $str = substr(0, $string_length, $str);
                $str .= '...';
        }
        return($str);
}

Word Chopper

// The following will limit a string to $max_words words
// usage: $var = word_chop($str(string), $max_words(limit of words, default: 15));


function word_chop($str, $max_words = 15) {
        $e = explode(' ', $str);
        $w = count($e);
        if($w > $max_words) {
                $str = '';
                for($i=0;$i<$max_words;$i++) {
                        $str .= ' '.$e[$i];
                }
        $str .= '...';
        }
        return($str);
}

Make Title Case

// Run text throughthis to make it title case
// "from russia with love" becomes:
// "From Russia with love"


function title_case($title) {
  // Our array of 'small words' which shouldn't be capitalised if
  // they aren't the first word.  Add your own words to taste.
  $smallwordsarray = array(
    'of','a','the','and','an','or','nor','but','is','if','then','else','when',
    'at','from','by','on','off','for','in','out','over','to','into','with'
    );
  // Split the string into separate words
  $words = explode(' ', $title);
  foreach ($words as $key => $word)
  {
    // If this word is the first, or it's not one of our small words, capitalise it
    // with ucwords().
    if ($key == 0 or !in_array($word, $smallwordsarray))
      $words[$key] = ucwords(strtolower($word));
  }
  // Join the words back into a string
  $newtitle = implode(' ', $words);
  return $newtitle;
}

Make Sentence Case

// Run text through this to make it sentence case...
// "the pretty pink sandwich" becomes:
// "The pretty pink sandwich"


function sentence_case($s) {
   $str = strtolower($s);
   $cap = true;
   for($x = 0; $x < strlen($str); $x++){
       $letter = substr($str, $x, 1);
       if($letter == "." || $letter == "!" || $letter == "?"){
           $cap = true;
       }elseif($letter != " " && $cap == true){
           $letter = strtoupper($letter);
           $cap = false;
       }
       $ret .= $letter;
   }
   return $ret;
}

Proper Ending for Numbers (i.e. 2nd, 3rd, 8th)

// Run numbers through this to add proper endings (i.e. 2nd, 3rd, 8th)


 function nth_num($age, $small = 0) {
        $last_char_age = substr("$age", -1);
        switch($last_char_age) {
                case '1' :
                        $th = 'st';
                        break;
                case '2' :
                        $th = 'nd';
                        break;
                case '3' :
                        $th = 'rd';
                        break;
                default :
                        $th = 'th';
                        break;
        }
        if ($age > 10 && $age < 20) $th = 'th';
        if (0 == $small) $niceage = $age.$th;
        if (1 == $small) $niceage = $age."$th";
        return $niceage;
        }

Curly Quote Maker

// Run text through this function to make curly quotes


        function curl_me($curlme) {
        // This should take care of the single quotes
        $curlme = preg_replace("/'([dmst])([ .,?!\)\/<])/i","’$1$2",$curlme);
        $curlme = preg_replace("/'([lrv])([el])([ .,?!\)\/<])/i","’$1$2$3",$curlme);
        $curlme = preg_replace("/(?\s+)'((?:[^ >])?(?:.*?)(?:[^=]))'(\s*[^>&])/Ss","$1‘$2’$3",$curlme);

        // time for the doubles
        $curlme = preg_replace('/(?]])((?:.*?)(?:[^=])?)"(\s*[^>&])/Ss',"$1“$2”$3",$curlme);
        // multi-paragraph
        $curlme = preg_replace('/

"(.*)<\/p>/U',"

“$1

",$curlme); // not a quote, but whatever $curlme = str_replace('Ñ','',$curlme); $curlme = str_replace('Ð','',$curlme); return $curlme; }

Mask address bar so always set to yourdomain.com

// You can use frames to mask your address bar so it always shows www.yourdomain.com when viewing your pages. Use the frameset below in your default page (index.htm). In this example, the frameset will load home.htm and start your site from there. But the address bar will stay showing www.yourdomain.com.

<frameset rows="*">
<frame src="home.htm">
frameset> 

Ensure your page is not opened in a frame

// If you want to make sure your website is not opened in a frame from some other website, put this JavaScript in the of your html page:

<script type="text/javascript">
if (top.location != self.location) { top.location.href = self.location.href; }
script>

Backup a single MySQL table

// Use this to take a single table backup, with elements in double quotes delimited with a comma:

while ($row = mysql_fetch_array($query,MYSQL_NUM)) $output .= "\"" . implode("\",\"",str_replace("\r\n"," ",$row)) . "\"\r\n"; echo $output; // or write $output to a file

CSS "Popup Window"

// Example of a css based "popup window"

        Click <a onmouseover='this.style.cursor="pointer" ' onfocus='this.blur();' onclick="document.getElementById('PopUp').style.display = 'block' " ><span style="text-decoration: underline;">herespan></a>
        <div id='PopUp' style='display: none; position: absolute; left: 50px; top: 50px; border: solid black 1px; padding: 10px; background-color: rgb(255,255,225); text-align: justify; font-size: 12px; width: 135px;'>
        This is a CSS Popup that can be positioned anywhere you want on the page and can contain any test and images you want.
        <br />
        <div style='text-align: right;'><a onmouseover='this.style.cursor="pointer" ' style='font-size: 12px;' onfocus='this.blur();' onclick="document.getElementById('PopUp').style.display = 'none' " ><span style="text-decoration: underline;">Closespan></a>div>
        </div>

Remove IE scrollbar place holder on non-scrolling pages

// If your page does not need to scroll, in certain situations IE may still show the browser gutter (placeholder for the scrollbar). Remove with this...

html {overflow:auto;}
« Newer Snippets
Older Snippets »
32 total  XML / RSS feed