May 16, 2008

PHP: Formatting dates

Formatting dates can be tricky for the beginning PHP programmer, here’s a few simple examples that will get you started. I’ll go over how to convert from database date formats to traditional date formats and back again, as well as how to test a date using regular expression matching…

March 14, 2008

Socket To Me, One More Time

Sorry, I couldn’t resist. I’ve been testing receiving data through the socket server I talked about in my last post. If you are testing without a steady stream of incoming data, you may need to generate your own UDP packets to send to your socket server. Here’s a simple script to open the socket on the other end. Keep in mind there is no back and forth communication with the User Datagram Protocol (UDP).

Create the socket, see the PHP documentation on which settings to use, these are for a UDP connection.

#!/usr/bin/php -q
<?php
/* create a socket in the AF_INET family,
using SOCK_DGRAM for udp connections */
$mysock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);

Specify your host and port, and put together a packet string to send:

Socket To Me

Sockets are the building blocks of networking connections between computers. Today I want to go though how to create a UDP listener (or socket server) in PHP. I recently had to do this to receive GPS data from some black boxes, and had a hard time finding examples of this type of UDP socket server. First of we need to specify who/where this socket will be created for; the host and port.

Pick a port that is higher than 1024, those below that are reserved.

#!/usr/bin/php -q
<?php
$address = "0"; //glom onto all addresses
$rport = "1228"; //receiving port

You should make $address = “127.0.0.1″ if you only want to allow connections on the localhost…

February 18, 2008

The Power of Regular Expressions

Regular expressions can be somewhat daunting to the beginning programmer. They really do look scary. But just like the back of your math book, it feels pretty good to look back and realize how far you’ve come. I’m going to give you an example of the power of regular expressions, and this really only glosses over the surface of them. If you can master regular expressions, this will seriously save you many lines of code, and a lot of grief. Take a look at the following two examples of validating a number in PHP:

February 8, 2008

If it ain’t broke, make it better

It’s finally time to rewrite that program that everyone is using. You know it’s been overdue for a while. You have some great ideas that you know will make it better, faster, more secure, right? You’ve been putting it off because you know it’s a big job. But now you have time, why don’t you dive right in? Rewriting your code is a really good exercise anyways, there are so many ways to do the same thing, improvement is just waiting for you to take a stab at it. But before you do, let’s go over a few steps to make the new app better and not worse.

February 7, 2008

Simple steps to using the Google Maps API

I really like maps, and no one does maps better than the Google Maps API. Today I’ll go over a few simple steps to getting started with the Google Maps API.