Archive for the ‘computery stuff’ Category

area code lookup tool

Friday, February 29th, 2008

For those of you who might be interested, I've slapped together a little command-line area code lookup tool. It's a really simple couple of scripts using grep to locate what you're searching for. It works on Unix-like systems with bash installed (Linux, OS X, etc).

You can download it here. Follow the enclosed instructions to install and use it.



geek stuff - handling Microsoft Word text in web development

Tuesday, November 13th, 2007

Note: Please contribute to this code in comments, if you find mistakes or know of improvements.

It's surprising to most people - especially computer programmers, it seems - that the English language actually has more than one kind of single quote and one kind of double quote. In fact, if you go back to single-quote characters in old-school typography, you find: (a) the opening single quote, (b) the closing single quote, (c) the possessive apostrophe, and (d) the contraction apostrophe. "Straight" quotes and apostrophes are a later invention, to reduce the number of keys on a keyboard.

Many modern word processing programs such as Microsoft Word automatically insert these special characters into text while the user is typing. Word calls them "smart quotes," and refers to fancy em dashes and the like as "symbols." These characters are very pretty in printed documents; in web development, however, they're a bit of a nightmare. It's something of a de facto workflow for users to copy Word documents and paste them into forms for blogs, content management systems and the like; smart quotes end up rendering as "question marks," because web browsers don't know what to make of them.

If you're experiencing this problem, you may hope for a nice "character set" solution in a meta tag (charset=xxx). Stop. Seriously. There's no character set at the moment that will both solve this problem and render well in most browsers.

The ideal solution is to convert Word documents into plain text before copying and pasting; unfortunately, this has little to do with how most people work. Therefore, if you're a PHP developer, you might need a function to replace these special characters. There are many proposed functions out there for exactly this task; the one I'm posting here is just the one that works for me.

<?php
function fixSmartQuotes($string){ 
    
$pre chr(226).chr(128);
    
    
$search = array( $pre chr(152),
                    
$pre chr(153),
                    
$pre chr(156),
                    
$pre chr(157),
                    
$pre chr(147),
                    
chr(145), 
                    
chr(146), 
                    
chr(147), 
                    
chr(148), 
                    
chr(150),
                    
chr(151),
                    
chr(130),
                    
chr(133),
                    
chr(152),
                    
chr(154),
                    
chr(160)
                    ); 
 
    
$replace = array( "'",
                             
"'",
                             
'"',
                             
'"',
                             
'-',
                             
"'"
                             
"'"
                             
'"'
                             
'"'
                             
'–',
                             
'-',
                             
"&#8218;",
                             
"&#8230;",
                             
'-',
                             
",
                             
' ' ); 
                    
    return 
str_replace($search$replace$string); 

?>


I'll explain this in detail a little later.



geek stuff: IE6 “Page cannot be displayed” fix in PHP

Thursday, September 20th, 2007

If you're working with forms in PHP, and especially if you're working with forms that have multiple steps, you've probably run across the "back button" problem in IE6. Here's what it looks like:

1) you fill out a form and click the "submit" button - this takes you to another page.

2) you click the "back" button.

3) IE6 takes you to an error page that says:

"Page could not be displayed"

and/or:

Warning: Page has Expired
"The page you requested was created using information you submitted in a form. This page is no longer available. As a security precaution, Internet Explorer does not automatically resubmit your information for you. To resubmit your information and view this Web page, click the Refresh button."

The problem is this: you want to be able to return to the previous page without resubmitting form data, AND with the form data still displayed as you've entered it. This, of course, is a caching problem. There are numerous proposed solutions for this online, but surprisingly few of them work well. Here's the one that works for me:

<?php
header
("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0″false);
session_start();
header("Cache-Control: private");
?>




geek stuff: passing parameters to onreadystatechange in AJAX

Thursday, September 6th, 2007

Maybe everyone knows this, but I've seen very little about it online. The vast majority of AJAX-oriented functions looks something like this:

var xmlHttp

function showHint(str)
{
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML=""
  return
  }
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Browser does not support HTTP Request")
  return
  } 
var url="gethint.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)


This function (taken from a w3schools tutorial) calls a function named stateChanged() when it receives data. stateChanged handles the display of response data, and looks like this:

function stateChanged() 

if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
 } 
}

Note that this function accepts no parameters, and for good reason - if you attempt to pass parameters to it in the first function, like so:

xmlHttp.onreadystatechange = stateChanged('hello world')

… everything will break.

If you need to pass parameters at onreadystatechange (and I often need to), here's how you do it:

xmlHttp.onreadystatechange = function(){ stateChanged('hello world'); };

and then you modify stateChanged() to take parameters as needed:

function stateChanged(param) { 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
         document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
     } else {
        document.getElementById("txtHint").innerHTML=param;
    }
}

And Bob's your uncle.



Meteorology

Saturday, August 11th, 2007

Meteorology is the science of angelic war. But it appears that the citizens of heaven are taking a cease-fire, because today is supposed to be beautiful.

Someday, I'm going to have to actually organize all the toys and tidbits on this site. Here are some of my favorite toys:

There's also the Google Walkabout, which is an experiment in random limited search - it will be more interesting someday.

I've spent a fair amount of time experimenting with APIs for various online communities, with the notion that the evolution of the "semantic web" might represent the evolution of a collective mind. Towards that end, I developed the Flickr Free Word Association application. As you very likely know, Flickr allows its users to associate tags with every image they post. Flickr then calculates which tags are related to which by the frequency by which they appear together (I think). The free word association application takes a word you enter, treats it as a tag, and returns a related tag. It's an interesting way of exploring the cognitive associations the entire Flickr community makes. A caveat about the freeword association application, however: Flickr's vocabulary isn't as extensive as one might like. When the application encounters a word that Flickr doesn't know, it uses the dict protocol to look up synonyms in an online dictionary. So it's not pure Flickr, sure, but it's still kinda neat.