Saturday, February 28, 2009

Phonetic Alphabet

It seems to me that most people have trouble remembering the phonetic alphabet (probably myself included). Unless you're a pilot or air traffic controller, it's unlikely that your job requires you to have it memorized. People get by with whatever word comes to mind ("A as in Adam"), but this seems like an opportunity to write some code.


For maximum reusability, I'm going to write my function to convert individual characters at a time. Looping through the characters of the string will occur outside the function. The function is too long to paste here in its entirety, but it will be posted on Snipplr and linked at the bottom of this entry.


PHP has key/value arrays where we can specify any keys we want, so we'll declare an array containing all the conversions and use the input to retrieve the correct string from the array. ASP's arrays don't have this flexibility; we could achieve this with a Dictionary scripting object, but I'm concerned about the overhead associated with that. I'm going to use a Select...Case statement (AKA switch statement), but there is a chance that this is actually not any better. I'll leave the determination of this as an exercise to the reader. Also left as an exercise to the reader is the writing of the looping code.


Saturday, February 21, 2009

Add and strip slashes

Good programmers know that they can't trust user-inputted data. We do as much as we can to validate and try to prevent bad data from getting into our applications. But there is even some perfectly valid data that can cause trouble, especially when you're dealing with databases. The single apostrophe is a common occurrence and gotcha for the newbies when they discover the tail end of their string didn't make it into the database.


Our PHP programmer friends have a few different functions at their disposal. Native to PHP are the addslashes() and stripslashes() functions, which basically do what they say. In more common usage these days is the MySQL-native function, mysql_real_escape_string(), which can handle a wider variety of situations. I'm going to go a little bit further and handle the backspace and horizontal tab characters.


The regular expression we'll be using to add slashes is ([\000\010\011\012\015\032\042\047\134\140]), and to remove slashes we'll use \\([\000\010\011\012\015\032\042\047\134\140]). The only difference is the extra pair of slashes at the beginning. These handle, respectively: null, backspace, horizontal tab, new line, carriage return, substitute, double quote, single quote, backslash, and grave accent.


Saturday, February 14, 2009

htmlspecialchars()

If you rigoursly validate your HTML like I do, you've probably seen many times the warning about HTML entities. Inevitably an ampersand makes its way into your code without being properly encoded. It's almost always in a hyperlink where you're trying to pass a QueryString variable or two.


somefile.asp?this=that&blah=meh


That ampersand needs to be encoded as &. Likewise, double quotation marks need to be encoded as " and the greater than and less than symbols need to be encoded as > and < respectively. If you allow users to write things on your web site, then you need a programmatic solution. Our PHP programmer friends have one in their toolbox which we can borrow.


  1. function htmlspecialchars(someString)
  2.     ' Critical that ampersand is converted first, since all entities contain them.
  3.     htmlspecialchars = replace(replace(replace(replace(someString, "&", "&"), ">", ">"), "<", "<"), """", """)
  4. end function
  5. function htmlspecialchars_decode(someString)
  6.     htmlspecialchars_decode = replace(replace(replace(replace(someString, "&", "&"), ">", ">"), "<", "<"), """, """")
  7. end function

View ASP implementation on Snipplr

Saturday, February 7, 2009

Difference between two stardates

As promised last week, I rejigged my code to calculate the difference between two stardates into a proper function and translated it to ASP. I don't have access to an IIS server right now, so I didn't have a chance to test the ASP version yet, but I'm fairly confident that it works. Let me know if it doesn't!

I won't post the source code directly into this post like I usually do, but as usual it will be available on Snipplr. Expect to see this happen more often in the future. However, I'll talk briefly about what's going on under the hood.

The incoming stardates are just strings, and we split them up based on the official format: first two characters to calculate the years, next three characters to calculate the days, and last character to calculate the hours. The order of the stardates is not important - the code checks which is larger. The same magic numbers from last week are used to get the real values, which are then subtracted. After rounding off any decimal places, the three values are formatted into a human-readable string and returned. It should be very straight-forward.