Monthly Archives: May 2014

WordPress Responsive Top Navigation Menu

Code a Responsive Top Navigation Menu in WordPress

As promised, here are the steps necessary to code a completely responsive top navigation menu into your current WordPress theme:

  1. If you know what your current theme is. Skip to next step, otherwise go to your WordPress admin dashboard and click on appearance/themes and see which theme is your active theme. Mine is twenty eleven so that is what I will reference in this tutorial, but the same instructions apply to any WordPress theme.
  2. Open the header.php file for your theme in a text editor or from the WordPress admin editor by going to appearance/editor and click on the header.php file name in the right column of the page.
  3. In header.php look for a meta tag for devices. In twenty eleven it looks like this:
    <meta name="viewport" content="width=device-width" />

    And change it to look like this:

    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  4. Now open your single.php file and search for the nav tag which should be at line number 17 or so. Delete everything in between the opening nav tag and ending nav tag including the tags themselves and replace them with:
    <nav><?php wp_nav_menu( array( 'theme_location' =>  'primary' ) ); ?></nav>
  5. Open your theme’s style css file and append the following CSS styles to the end of it:
    nav select{
      display: none; /*= Force dropdown navigation to hide on desktop = */
    }
    @media (max-width: 960px)  
    {
      nav ul     { display: none; } /*= Force normal horizontal navigation to hide mobile devices and ipad = */
      nav select { display: inline-block; } /*= Force dropdown navigation to get displayed on mobile devices and ipad = */
    }

That’s all there is to customizing your WordPress top navigation menu to work great on iPod, iPhone and other mobile devices. Go ahead and check it now on a mobile platform and verify that it works.

Install Command Line Whois on Linux Server

If the Whois command, as demonstrated  below, doesn't work from the command line, then you likely have to install the Whois library.

Install Whois from Command Line

  1. Open a command prompt and log in to your Linux server. I use Putty to do this.
  2. change to the super user. I used sudo su or su sudo but some servers may have slightly different commands for this. Google it if you are not sure and those commands don't do the trick, or you could simply proceed all future commands with sudo if all else fails.
  3. From the command Prompt, type: yum install whois

Possible issues installing Linux Command Line Whois

WHen I installed I got an error message that said that the following:

Existing lock /var/run/yum.pid: another copy is running as pid 27256.
Another app is currently holding the yum lock; waiting for it to exit...
  The other application is: yum-updatesd-he
    Memory :  29 M RSS (328 MB VSZ)
    Started: Mon May 19 19:18:12 2014 - 13:54 ago
    State  : Running, pid: 27256

So I had to learn how to kill that process and try again. Simply type:

kill ####

where #### is replaced by the pid number noted in the error. In my case it was 2756 as you can see in the red error text above.

You may have to also try these commands from command prompt if it's not working yet:

1) service yum-updatesd stop
2) chkconfig yum-updatesd off

Using Linux Command Line Whois

Once installed, Linux Whois commands are fairly easy to use. For example, if you wanted the whois data for the Jafty.com domain from the whois server, whois.serverplan.com, then use the following commands:

 whois -h whois.serverplan.com jafty.com
or

whois  jafty.com

Note the -h parameter in the first example above. Only use that if you have a specific need to specify the host of the whois server you want to query, otherwise, use the simpler second example of "whois domain.com" and you'll be good.

Here are options you can use with the Whois command as well:

Options

-h HOST Connect to WHOIS database host HOST.
-H Suppress the display of legal disclaimers.
-p PORT When connecting, connect to network port PORT.
--verbose Operate verbosely.
--help Display a help message, and exit.

 

Whois Alternative, jWhois

You can also use the very similar jWhois library if whois doesn't do the trick for you. Simply use the following command to install it instead and use it similarly:

yum -y install jwhois

This worked for me(jwhois) wen the above whois instructions Did Not.

Coding a PHP Whois App that uses the Whois Command

The following PHP code will work with either whois or jWhois out of the box:

<?php
//whois servers that got what we need:
//whois2.softlayer.com
//whois.webnames.ca
//whois.tppinternet.com
//whois.totalregistrations.com
//whois.softlayer.com
//whois.serverplan.com
//trying:
//whois.godaddy.com have a try with below email!!! use with current whis first then this
function whoEmail($curdomn){
$whoserver = 'whois.softlayer.com';//'whois.serverplan.com';//'whois.webnames.ca';
$comand = "whois -h $whoserver $curdomn";
$output = shell_exec($comand);
echo "Output: $output<hr>";
$outarra = explode('Registrant Email:',$output);//key 1 is email plus xtra
$output2 = $outarra[1];
$outarra2 = explode('Registry',$output2);//key 0 is email so trim it
$emailout = trim($outarra2[0]);
//echo "Email: $emailout<hr>";
//echo "$curdomn produced:<pre>$output</pre><hr>";
return $emailout;
}//end whoEmail function.

//test function:
$e = whoEmail('dirm.com');
echo "Email: $e<br>";

?>

Notice at the top of my PHP script there are commented out whois server URLs. Feel free to experiment because I found that all of the ones listed there work with different domains. You can add more as well and increase the apps functionality by adding more code of course.

Summary

While I did find information online regarding the whois library and how to installl and use it, I found that it did not work and had to use the second option on this page, the jWhois library, which worked fine. I also went on to show you how you can write a nifty PHP script to work with the whois command, whether you used whois or jwhois, it will work the same.

 

PHP Functions to Validate Domain Names and IP Addresses

If you are similar to me in that you like to create PHP applications for the domain name industry or you have a need to validate domains or IPs, then this tutorial is for you.

Validate a Domain Name with PHP

The Valid Domain Function:

function validDom($domain) {
    if(!preg_match("/^([-a-z0-9]{2,100})\.([a-z\.]{2,8})$/i", $domain)) {
    return false;
    }
return $domain;
}//end validDom function.

Executing the ValidDom Function:

if(ValidDom($domain)) {
echo "<p style='color:green'>$domain is valid.</p>";
}else{
echo "<p style='color:red'>ERROR! $domain is NOT a valid domain name.</p>";
}

Validate an IP address with PHP

 

The Valid IP function:

function validIP($ip) {
$ipnums = explode(".", $ip);
    if(count($ipnums) != 4) {//if there is not 4 numbers to the IP return false
    return false;
    }
    
    foreach($ipnums as $ipnum) {//for ea. IP number make sure it's less than 256:
        if(!is_numeric($ipnum) || ($ipnum > 255)) {
        return false;
        }
    }
return $ip;//if made it this far, returns valid IP
}//end validIP function

Executing the ValidIP Function:

if(ValidIP($ip)) {
echo "<p style='color:green'>$ip is valid.</p>";
}else{
echo "<p style='color:red'>ERROR! $ip is NOT a valid IP address.</p>";
}

The Ultimate PHP WhoIs Parsing Code – Jafty Whois

As far as I am concerned, this is the holy grail of PHP Whois apps and I have tried a lot of them. I of course had to make my own to get what I wanted, but I used what I took in from experimenting with all the popular choices out today to come up with JAFTY WHOIS.

The trick was to keep it as simple as possible and get it to run as many consecutive queries as possible. The purpose of the code is to fetch Whois data for a quantity of domain names. It gets such data as the domain owner's email address, contact information, expire and creation date.

The Code

I developed the ultimate Jafty Whois code with two key functions. The first is called "queryWhois" and is the function that does the actual query to the whois server. The second function, called "runDom", basically runs the "queryWhois" function. The job of the "runDom" function is to monitor the "queryWhois" function and sometimes run it again based on what it returns.

Here are the two functions as they are in the working demo (Contact Ian at linian11@yahoo.com for complete demo code):

function queryWhois($domain,$whoisserver = "whois.verisign-grs.com") {
$port = 43;
$timeout = 10;
$fp = @fsockopen($whoisserver, $port, $errno, $errstr, $timeout) or die("fsocketopen Error " . $errno . " - " . $errstr);
fputs($fp, $domain . "\r\n");
$out = "";
    while(!feof($fp)){
    $out .= fgets($fp);
    }
fclose($fp);
$res = "";
    if((strpos(strtolower($out), "error") === FALSE) && (strpos(strtolower($out), "not allocated") === FALSE)) {
    $rows = explode("\n", $out);
        foreach($rows as $row) {
        $row = trim($row);
            if(($row != '') && ($row{0} != '#') && ($row{0} != '%')) {
            $res .= $row."\n";
            }
        }
    }
return $res;
}//end queryWhois function.

function runDom($domain){
$whoisserver = ".com";//just for showing note below, not actual server...
$result = queryWhois($domain);
    if(!$result){
    return "Error: No results found for $domain!";
    }else{
    //look for "Whois Server:" indicating we need to get results from a diff server:
        while(strpos($result, "Whois Server:") !== FALSE){
        preg_match("/Whois Server: (.*)/", $result, $matches);
        $secondary = $matches[1];
            if($secondary) {
            $result = queryWhois($domain,$secondary);
            $whoisserver = $secondary;
            }
        }//end while looking for secondary whois server.
    }
return "<h2>$domain results from $whoisserver server:</h2>" . $result;
}//end runDom function

Why does the queryWhois function need monitored?

Great question and that's the secret to this code's success. Lets take the raw queryWhois function and run it by itself without the "runDom" functon and see what happens. There are two likely scenarios, the first will return standard whois results and that's fine, but the second will return something like the below example. That's bad because it doesn't contain the whois data we are ultimately looking for! However, it does contain the key to getting that data. It contains the whois server you need to query instead. This is done with the queryWhois function too, but it needs to be told to run again with the a different whois server, so in comes the "runDom" function to tell it to run again when this secondary whois server is spotted, passing that whois server URL as a variable to the queryWhois function like: queryWhois($domain, $serverURL). The $serverURL variable would be set to the new whois server found while running the queryWhois function the first time.

Example results of queryWhois function when a secondary whois server is found instead of domain name's whois data:

--------------------Start queryWhois test results------------------

Whois Server Version 2.0
Domain names in the .com and .net domains can now be registered
with many different competing registrars. Go to http://www.internic.net
for detailed information.
Domain Name: AAAX.COM
Registrar: DOMAIN.COM, LLC
Whois Server: whois.domain.com
Referral URL: http://www.domain.com
Name Server: NS1.SEDOPARKING.COM
Name Server: NS2.SEDOPARKING.COM
Status: clientTransferProhibited
Status: clientUpdateProhibited
Updated Date: 18-nov-2013
Creation Date: 01-dec-1999
Expiration Date: 01-dec-2014
>>> Last update of whois database: Sun, 18 May 2014 06:40:14 UTC <<<
NOTICE: The expiration date displayed in this record is the date the...*

---------------------End queryWhois test results----------------

*NOTE:  where you see ...* at the end of the above test results, I shortened the results because the remaining text was irrelevant for this demonstration.

Notice in the above example test results where I highlighted the whois server text in orange bold text. That is what the runDom function searches for, and if the text "whois server:" is found, it knows to rerun the queryWhois function a second time using the whois server named after the colon.

Example results of runDom function controlling the queryWhois function so that it runs secondary Whois server found during first run of queryWhois function:

------------------------Start test results--------------------

Domain Name:

aaax.com results from whois.domain.com server:

Domain Name: AAAX.COM
Registry Domain ID:
Registrar WHOIS Server: whois.domain.com
Registrar URL: www.domain.com
Updated Date: 2013-11-18 09:04:07
Creation Date: 1999-12-01 15:20:53
Registrar Registration Expiration Date: 2014-12-01 15:20:53
Registrar: Domain.com, LLC
Registrar IANA ID: 886
Registrar Abuse Contact Email: compliance@domain-inc.net
Registrar Abuse Contact Phone: +1.6027165396
Reseller: MyDomain
Reseller: support@mydomain-inc.com
Reseller: +1.8004057875
Domain Status: clientTransferProhibited
Domain Status: clientUpdateProhibited
Registry Registrant ID:
Registrant Name: TODD BERNHARD
Registrant Organization: Domain Owner
Registrant Street: 20 Barclay Square Drive
Registrant City: Rochester
Registrant State/Province: NY
Registrant Postal Code: 14618
Registrant Country: US
Registrant Phone: 585475.1030
Registrant Phone Ext:
Registrant Fax: 585475.1030
Registrant Fax Ext:
Registrant Email: domain@hqlabs.com
Registry Admin ID:
Admin Name: TODD BERNHARD
Admin Organization: Domain Owner
Admin Street: 20 Barclay Square Drive
Admin City: Rochester
Admin State/Province: NY
Admin Postal Code: 14618
Admin Country: US
Admin Phone: 585475.1030
Admin Phone Ext:
Admin Fax: 585475.1030
Admin Fax Ext:
Admin Email: domain@hqlabs.com
Registry Tech ID:
Tech Name: TODD BERNHARD
Tech Organization: Domain Owner
Tech Street: 20 Barclay Square Drive
Tech City: Rochester
Tech State/Province: NY
Tech Postal Code: 14618
Tech Country: US
Tech Phone: 585475.1030
Tech Phone Ext:
Tech Fax: 585475.1030
Tech Fax Ext:
Tech Email: domain@hqlabs.com
Name Server: NS1.SEDOPARKING.COM
Name Server: NS2.SEDOPARKING.COM
DNSSEC: Unsigned
URL of the ICANN WHOIS Data Problem Reporting System: http://wdprs.internic.net/
>>> Last update of WHOIS database: 2013-11-18 09:04:07 <<<
Registration Service Provider:
MyDomain, support@mydomain-inc.com
+1.8004057875
This company may be contacted for domain login/passwords,
DNS/Nameserver changes, and general domain support questions.

-------------------------End test results---------------------

Summary

Sometimes the queryWhois function will return the results the first time and doesn't need the runDom function to extract the secondary whois server and rerun it. So, it should be noted that sometimes it returns results and sometimes it returns a string of text that most likely contains the actual whois server to query for the domain name passed to it. Without the important runDom function, the queryWhois function would only work approximately one out of five times. If you require assistance with Jafty Whois or would like a custom application built based on it, contact Ian L. of Jafty.com at linian11@yahoo.com

Post Form Data with CURL and get Results

Today, I am doing another domain name related project(on of my specialties). I need a fresh PHP function to get a domain name's whois data from. It's time to get dirty with CURL because all my PHP whois functions are failing lately after about 500 queries or less, so let's do this!

PHP CURL POST FUNCTION

The first thing I needed was a decent CURL function to post to a URL and get results. Here is what I found:

<?php
function curlPost($url, $data){
// check for URL and data:
    if(empty($url)){
        return 'Error: missing URL';
    }
    if(empty($data)){
        return 'Error: missing Data';
    }

//get the data for the POST and put in URL:
$fields_string = '';
foreach($data as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//Set the URL, no. of POST vars and POST data:
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($data));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//timeout after 10 secs(change as needed):
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
//curl_setopt($ch,CURLOPT_HEADER,false);
//Set to return data instead of printing:
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
//pass user agent:
curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

//execute post:
$result = curl_exec($ch);

//close connection:
curl_close($ch);
return $result;
}//end curlPost function.

echo "<h3>Whois test</h3>";
$data = array('d43b' => 'jafty.com');
echo curlPost('http://www.secret.net/services/whois.php', $data);

?>

Okay, I know, unfair, I took out the target domain, but find your own lol. I don't want everyone exploiting the poor server. But I was still limited with this technique because the server I queried had a maximum number of queries allowed, so I still need to find out how to overcome this.

Getting the Word Out with Reciprocal Links

Jafty.com loves to do reciprocal links to get the word out about our services. If anyone is interested in link trading with us, comment on this page or just send Ian L. of Jafty.com an email at linian11@yahoo.com and you'll get a fast reply. You may also follow these simple instructions to trade links with Jafty.com:

  1. Place a link to Jafty.com or Jafty.com/blog on your website(preferably just http://jafty.com/). Put it in a place where I can easily find it.
  2. Comment on this page letting me know where you put the link and give me the link text you'd like to appear on Jafty.com in return. It will be done fast!

Jafty.com Link Partners:

http://www.web-development.com - a great place to place a link if you are a web developer and trying to get known!

http://www.jafty.com/ - The authority in Web Development Services from A to Z! Our motto: ANYTHING IS POSSIBLE!

http://www.petermichaelsonjewellery.com.au - Your online source for GIA certified triple X diamonds.

IMPORTANT NOTE to link partners:

Please leave a comment on this page with a note indicating where you have placed your link to jafty.com and I will in turn be sure to add your link to my site as well. I will check this page from time to time and verify that all links are indeed reciprocal. Any links I have placed here that do not also link to Jafty.com may be removed if the site doesn't contain a link back to http://jafty.com. Thank you - Ian L.

 
Want to learn more about SEO? Check out these relative books from Amazon:

How to Make a Custom 404 Error File

I guess different servers do 404 error files different ways. I am working on an Apache server, so that is what we will be covering here.

Create a custom HTML error File

This part is simple, use regular old HTML along with some CSS style to create a normal web page with whatever content you'd like users to see when encountering a 404, file not found error on your web site. For the purpose of this tutorial, you could simply copy and paste the following line into a file and name it custom404.html:

ERROR: No file found! Please try again.

Of course, feel free to use all HTML markup and CSS you like, but for demonstration purposes, the above line of text will work just fine as well.

Edit the .htaccess File

You can find a file named .htaccess in your root directory and if not, just create a new file and name it .htaccess. Note that .htaccess is a file extension and should have a dot in front of it. It's like a file without a name basically using the .htaccess extension. Weird, I know, but that's how it's done. Also be careful if you use notepad because it will try to append .txt to the file name and it won't work like that, so if you have that issue, try entering ".htaccess" with the quotes when saving the file and it should understand. So whether you found the .htaccess file or had to create a fresh one, simply append this to the end of your file and save as I just described:

ErrorDocument 404 /custom404.html

Then upload your custom404.html file to the servers web root directory and you are done! If it doesn't work, try restarting the server before moving on to the next method.

 

Getting a Google Adwords API Key

Today I found out it is not so easy to get a Google Adwords API key if you are a Developer without any or many Adwords clients to manage. I will lay forth the process here while I attempt to obtain a developer key in case it helps anyone in the future. I also like to have this documented for my own reference.

Signing up for Adwords API Key:

  1. You'll need to have an MCC (My Client Center) account to request an API key. If you don't already have an MCC account, you can go signup for one at:  http://www.google.com/intl/en/adwords/myclientcenter/ (link will open a new window so you can stay here and read the rest of my instructions). You can click on the button on the right of that page to sign up or the link under it to sign in if you already have an account.
  2. Simply follow the instructions at the above link to set up your MCC account. Please note that if you do manage some Google Adwords accounts, unlike myself at this time, then you will be given preference for an API key according to posts I read online, so go here: https://support.google.com/adwords/agency/answer/7732 if you have accounts you can link already to your MCC account.
  3. Sign in to your MCC account here: https://adwords.google.com/ 
  4. After signing in to your MCC account, click the gear icon and select "Account Settings" from the drop down. Then on the resulting page, click on the "Adwords API Center" in the left column of the page.
  5. Follow the instructions for signing up for an API key. You will need to provide a credit card and personal information, so if you don't intend to use Adwords, it is pointless to sign up, so I'm not sure why they give you such a hard time to get an API key.
  6. After signing up for both an MCC account and an API key, you'll get an email. Mine said that I didn't have any clients that use Adwords and therefore they assumed I had no need for a developer token or API key, so they say if you still wish to go through with applying for the API key, to click on a link in the email to fill in a form to tell them why you should have an api key. I did this. I explained that I wanted to build a couple applications using the Google Adwords API for clients of mine, which was true, so I hope it works. I uploaded a very short description of the first tool I plan to build because they require you to upload either a screen shot or other file that describes the tool you intend to make. I will post more here when I get a reply. So far, all this was done on 5-11-2014, on a Sunday, so I'll try to remember to note the time it takes to get a reply to this second inquiry which came in the same day I applied for my MCC account and API key.

Building a Google Adwords API Tool

As I may have mentioned, my goal is to build a tool that uses the Adwords API, so while waiting for my response, I read that there are some pretty strict requirements for any tool you build using their API, You can read the min. requirements section to learn more Here: https://developers.google.com/adwords/api/docs/requirements.

Also they have some Terms and Conditions(click to read them) that you need to adhere to.  So if you get rejected or better yet, before you apply, read these and amend your tool's specs to follow both the terms of service and the minimum requirements as documented at the previous two links I have provided.

Installing PEAR on Amazon EC2 or Ubuntu Linux Server

I will show you a fast way to check if Pear exists on your Linux server and if not, we will install it.

Does PEAR Exist?

Open a command prompt to see if PEAR exists. You can do so by simply typing:
which pear
at the command prompt. If you get a response that looks similar to:
/usr/bin/pear
then you have PEAR installed, but if you get something similar to:
/usr/bin/which: no pear in....
then you do not have PEAR installed so read on!

Installing PEAR

On Ubuntu, you can use the following command from the command line:

apt-get install php-pear

If u don't have Ubuntu and are on a Linux server, use these two commands instead:

wget http://pear.php.net/go-pear.phar
php go-pear.phar

PHP_SELF Function to Get Current Page URL

You may use the PHP_SELF function to the your current page url, but pay attention to what it returns below.

Here are some examples:

Here is how to print the current page URL to your screen:
echo PHP_SELF;

If your script that includes PHP_SELF is at http://jafty.com/test/self.php, it would return:
/test/self.php

Handy Regex Notes for Notepad++

Here are some regex expressions I have found to be useful with notepad++ as a programmer:

  1. .*' - (dot star apostrophe), finds all  occurrences of "'s".
  2. .*word - finds all occurrences of any number of characters up to the word "word".
  3. word.* - finds all occurrences of the word "word" followed by any number of characters.
  4. [a-z][a-z][a-z].com  - finds all three letter .com domain names(lower case). Simply change the a-z to uppercase to look for uppercase 3 letter domains.
  5. [a-z][a-z].com - finds all two letter .com domain names in lower case. Change to A-Z for uppercase.
  6. [0-9][0-9][0-9].com - finds all three number .com domain names.
  7. Adult T - [0-9a-zA-Z ]+, - finds all occurrences of "Adult T -" followed by another word or number(or combo) and then a comma.
  8. .*[\|]  - Finds and replace all but the domain name in cases where ea. line in a file looks like: celebrification.com | scottludbrook@aanet.com.au(make sure that the regex ends in a space). Note that the pipe character is escaped with a backslash.
  9. ^\s - find are replace all blank lines. Deletes blank lines when search for this and replace with nothing.

Make all lowercase

While not a regex, I learned how to change all characters to lowercase using Notepad++ today as well. All you do is select the entire document or the part you want lowercase and right click then select "lowercase" and DONE!

Find & Replace Part of a String

Lets say you have a CSV file with these contents:
1 Adult T - Led Zeppelin, black, XL

2 Adult T - Motley Crue, black, Med

3 Adult T - Grateful Dead, tie die, Sm

...and you need to replace:

"Adult T - Grateful Dead," with "Grateful Dead T-Shirt,"

This is where the \1, \2, \3 abilties of Notepad++ version 6+ comes in very handy. First you define parts one and two in parenthesis, so as in the #7 example above you'd change it to this instead:
Search for:
(Adult T - )([0-9a-zA-Z ]+),
and replace with:
\2 T-Shirt,

The above tells Notepad to find the text "Adult T - " and any string after it until you hit a comma. Then in the last line above, it says to replace it with what is in the 2nd parenthesis and to add T-Shirt to it so "Adult T - Led Zeppelin," becomes "Led Zeppelin T-Shirt,". Works like a charm!

By reading and studying the above examples, you should be able to modify the regex expressions to do just about anything. I am using only simple regular expressions here to make it easier to understand because most of us have a terrible time understanding all the regular expression rules.

PHP Get State Abbreviations from Name or Abbreviations from State Names

<?php

function state_abbr($name, $get = 'abbr') {
//make sure the state name has correct capitalization:
    if($get != 'name') {
    $name = strtolower($name);
    $name = ucwords($name);
    }else{
    $name = strtoupper($name);
    }
$states = array(
'Alabama'=>'AL',
'Alaska'=>'AK',
'Arizona'=>'AZ',
'Arkansas'=>'AR',
'California'=>'CA',
'Colorado'=>'CO',
'Connecticut'=>'CT',
'Delaware'=>'DE',
'Florida'=>'FL',
'Georgia'=>'GA',
'Hawaii'=>'HI',
'Idaho'=>'ID',
'Illinois'=>'IL',
'Indiana'=>'IN',
'Iowa'=>'IA',
'Kansas'=>'KS',
'Kentucky'=>'KY',
'Louisiana'=>'LA',
'Maine'=>'ME',
'Maryland'=>'MD',
'Massachusetts'=>'MA',
'Michigan'=>'MI',
'Minnesota'=>'MN',
'Mississippi'=>'MS',
'Missouri'=>'MO',
'Montana'=>'MT',
'Nebraska'=>'NE',
'Nevada'=>'NV',
'New Hampshire'=>'NH',
'New Jersey'=>'NJ',
'New Mexico'=>'NM',
'New York'=>'NY',
'North Carolina'=>'NC',
'North Dakota'=>'ND',
'Ohio'=>'OH',
'Oklahoma'=>'OK',
'Oregon'=>'OR',
'Pennsylvania'=>'PA',
'Rhode Island'=>'RI',
'South Carolina'=>'SC',
'South Dakota'=>'SD',
'Tennessee'=>'TN',
'Texas'=>'TX',
'Utah'=>'UT',
'Vermont'=>'VT',
'Virginia'=>'VA',
'Washington'=>'WA',
'West Virginia'=>'WV',
'Wisconsin'=>'WI',
'Wyoming'=>'WY'
);
    if($get == 'name') {
    // in this case $name is actually the abbreviation of the state name and you want the full name
    $states = array_flip($states);
    }

return $states[$name];
}

//test function:
$state = "OHIO";
$state_a = state_abbr($state);
echo "<p>The abbreviation for $state is $state_a</p>";

$state = "ohio";
$state_a = state_abbr($state);
echo "<p>The abbreviation for $state is $state_a</p>";

$state = "Ohio";
$state_a = state_abbr($state);
echo "<p>The abbreviation for $state is $state_a</p>";

$state = "oHIo";
$state_a = state_abbr($state);
echo "<p>The abbreviation for $state is $state_a</p><hr />";

$state = "oh";
$state_a = state_abbr($state, "name");
echo "<p>The abbreviation $state is for $state_a</p>";

$state = "Oh";
$state_a = state_abbr($state, "name");
echo "<p>The abbreviation $state is for $state_a</p>";

$state = "OH";
$state_a = state_abbr($state, "name");
echo "<p>The abbreviation $state is for $state_a</p>";
?>