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

Dan Berlyoung

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

Convert Mac to Unix line endings in VI

If you open a text file in VI and see one big line with lots of '^M' where the line endings are supposed to be, use this command to fix it.
(Enter the the ^M by hitting ctrl-v and then the return key.)

:1,$s/^M/\r/g

Human Readable ls and df commands

Simple but useful to know...

To get a ls (file list) or df (disk free) to show in K,M or G instead of *huge* numbers of bytes without commas, use the -h flag.

ls -lh
df -h

Get all files in ftp server directory using WGET

It's a PITA to recursively get all files in a directory using ftp. Instead use wget.

wget -r ftp://account_name:password@example.com/directoryname

US States Pop-up

Simply a pop-up form element of all 50 US States. Save you creating your own.

            <select name="state">
              <option value="" selected="selected">Select a state...option>
              /option>
              <option value="AK">Alaskaoption>
              /option>
              <option value="AR">Arkansasoption>
              /option>
              <option value="CO">Coloradooption>
              /option>
              <option value="DC">District of Columbiaoption>
              /option>
              <option value="FL">Floridaoption>
              /option>
              <option value="HI">Hawaiioption>
              /option>
              <option value="IL">Illinoisoption>
              /option>
              <option value="IA">Iowaoption>
              /option>
              <option value="KY">Kentuckyoption>
              /option>
              <option value="ME">Maineoption>
              /option>
              <option value="MA">Massachusettsoption>
              /option>
              <option value="MN">Minnesotaoption>
              /option>
              <option value="MO">Missourioption>
              /option>
              <option value="NE">Nebraskaoption>
              /option>
              <option value="NH">New Hampshireoption>
              /option>
              <option value="NM">New Mexicooption>
              /option>
              <option value="NC">North Carolinaoption>
              /option>
              <option value="OH">Ohiooption>
              /option>
              <option value="OR">Oregonoption>
              /option>
              <option value="RI">Rhode Islandoption>
              /option>
              <option value="SD">South Dakotaoption>
              /option>
              <option value="TX">Texasoption>
              /option>
              <option value="VT">Vermontoption>
              /option>
              <option value="WA">Washingtonoption>
              /option>
              <option value="WI">Wisconsinoption>
              /option>
            select>

Get your Flash Movie's URL

This function returns the full URL of the .swf file. Simple enough.

myUrl = getProperty("", _url );

State name to 2 letter code

Simple one-trick-pony PHP function to take a state name (case insensitive) and return the 2 letter abbreviation.

        function state_to_twoletter( $state_name ) {
                
                $state = array();
                $state['ALABAMA']='AL';
                $state['ALASKA']='AK';
                $state['AMERICAN SAMOA']='AS';
                $state['ARIZONA']='AZ';
                $state['ARKANSAS']='AR';
                $state['CALIFORNIA']='CA';
                $state['COLORADO']='CO';
                $state['CONNECTICUT']='CT';
                $state['DELAWARE']='DE';
                $state['DISTRICT OF COLUMBIA']='DC';
                $state['FEDERATED STATES OF MICRONESIA']='FM';
                $state['FLORIDA']='FL';
                $state['GEORGIA']='GA';
                $state['GUAM']='GU';
                $state['HAWAII']='HI';
                $state['IDAHO']='ID';
                $state['ILLINOIS']='IL';
                $state['INDIANA']='IN';
                $state['IOWA']='IA';
                $state['KANSAS']='KS';
                $state['KENTUCKY']='KY';
                $state['LOUISIANA']='LA';
                $state['MAINE']='ME';
                $state['MARSHALL ISLANDS']='MH';
                $state['MARYLAND']='MD';
                $state['MASSACHUSETTS']='MA';
                $state['MICHIGAN']='MI';
                $state['MINNESOTA']='MN';
                $state['MISSISSIPPI']='MS';
                $state['MISSOURI']='MO';
                $state['MONTANA']='MT';
                $state['NEBRASKA']='NE';
                $state['NEVADA']='NV';
                $state['NEW HAMPSHIRE']='NH';
                $state['NEW JERSEY']='NJ';
                $state['NEW MEXICO']='NM';
                $state['NEW YORK']='NY';
                $state['NORTH CAROLINA']='NC';
                $state['NORTH DAKOTA']='ND';
                $state['NORTHERN MARIANA ISLANDS']='MP';
                $state['OHIO']='OH';
                $state['OKLAHOMA']='OK';
                $state['OREGON']='OR';
                $state['PALAU']='PW';
                $state['PENNSYLVANIA']='PA';
                $state['PUERTO RICO']='PR';
                $state['RHODE ISLAND']='RI';
                $state['SOUTH CAROLINA']='SC';
                $state['SOUTH DAKOTA']='SD';
                $state['TENNESSEE']='TN';
                $state['TEXAS']='TX';
                $state['UTAH']='UT';
                $state['VERMONT']='VT';
                $state['VIRGIN ISLANDS']='VI';
                $state['VIRGINIA']='VA';
                $state['WASHINGTON']='WA';
                $state['WEST VIRGINIA']='WV';
                $state['WISCONSIN']='WI';
                $state['WYOMING']='WY';

                // Canadian Provinces
                // edited 12-5-07
                $state['ALBERTA']='AB';
                $state['BRITISH COLUMBIA']='BC';
                $state['MANITOBA']='MB';
                $state['NEW BRUNSWICK']='NB';
                $state['LABRADOR']='NL';
                $state['NEWFOUNDLAND]='NL';
                $state['NORTHWEST TERRITORIES']='NT';
                $state['NOVA SCOTIA']='NS';
                $state['NUNAVUT']='NU';
                $state['ONTARIO']='ON';
                $state['PRINCE EDWARD ISLAND']='PE';
                $state['QUEBEC']='QC';
                $state['SASKATCHEWAN']='SK';
                $state['YUKON']='YT';

                return $state[strtoupper( $state_name )]; 
                
        }

Center something vert. and horz. in a web page using CSS

Here's how to center anything vertically and horizontally in a web page using CSS. Works with most all browsers that support CSS.

I adapted this from Jak psåt web, thanks!

<html>
        <head>
                <title>Center w/ CSStitle>
                
        
        
                
"outer">
"middle">
"inner"> your stuff here in center of page

explode() function for Actionscript

A simple version of the explode() function in PHP. It takes a string and splits it up into an array by splitting it at whatever character (or characters) you specify. For example, reading in a tab delimited text file. Will split it into lines by splitting on returns ("\r"). Then split up the lines by splitting on tabs ("\t").

Attribution: I didn't write this myself, I found it in a comment on one of the Actionscript on-line documentation pages.

function explode(separator:String, string:String) {

        var list = new Array();

        if (separator == null) return false;
        if (string == null) return false;

        var currentStringPosition = 0;
        while (currentStringPosition<string.length) {
                var nextIndex = string.indexOf(separator, currentStringPosition);
                if (nextIndex == -1) break;
                var word = string.slice(currentStringPosition, nextIndex);
                list.push(word);
                currentStringPosition = nextIndex+1;
        }
        if (list.length<1) {
                list.push(string);
        } else {
                list.push(string.slice(currentStringPosition, string.length));
        }
        return list;
}

print_r() for Actionscript

Here's a cheap little function to mimic the print_r() function from PHP in Actionscript. It's designed to work on most any array and will handle nested arrays through recursion.
//
// recursive function to print out the contents of an array similar to the PHP print_r() funciton
//
function print_a( obj, indent ) {
        if (indent == null) indent = "";
        var out = "";
        for ( item in obj ) {
                if (typeof( obj[item] ) == "object" )
                        out += indent+"[" + item + "] => Object\n";
                else
                        out += indent+"[" + item + "] => " + obj[item]+"\n";
                out += print_a( obj[item], indent+"   " );
        }
        return out;
}
// example call
trace( print_a( example_array ) );

Collapse Whitespace function for Actionscript

This function will strip out all tabs and return characters. It will also collapse all runs of multiple spaces down to one. It will also remove a leading and trailing spaces. It is similar to the php_strip_whiespace() function in PHP.

function collapseWhiteSpace( theString ) {
        theString =  theString.split("\r").join("");
        theString =  theString.split("\t").join("");
        while ( theString.indexOf("  " ) != -1 ) {
                theString= theString.split("  ").join(" ");
        }
        if (theString.substr(0,1) == " ") {
                theString = theString.substr( 1 );
        }
        if (theString.substr( theString.length-1, 1 ) == " ") {
                theString = theString.substr( 0, theString.length - 1 );
        }
        return( theString );
}

Parse out body content from html file in PHP

Parse out the body content of an html file. If no tags found, assume whole file is the content to be used.

        $body_content = join( "", file( $file_to_be_read ) );
        if (eregi( "(.*)", $t, $regs ) ) {
                $body_content = $regs[0];
        }

Reverse DNS from command line

Quick and easy way to look up a domain name given an IP address.

dig -x 17.254.3.183


Retreiving a Flash movie's domain name

Use the LocalConnection object to get the domain name of the server where the Flash movie is located.

var localDomainLC:LocalConneciton = new LocalConnection();
myDomainName = localDomainLC.domain();
trace( "My domain is " + myDomainName );



This will print out something like this:

My domain is example.com


Search for open files that won't allow you to unmount a server volume

lsof | grep "Office Server"

Lists out all open files on a given volume ("Office Server" in this case.) Good to find those peskey open files that won't let you eject a mounted network volume.
« Newer Snippets
Older Snippets »
14 total  XML / RSS feed