If, like me, you hose Apache on your Ubuntu LTS server when you finally get around to upgrading libssl, and PHP stops working...

You'll be needing this:

apt-get --reinstall install apache2 php5-mysql \
libapache2-mod-php5 mysql-server

(You're welcome)

Preparing a string for use in a PHP regular expression.

This blog post is just for me, so I can look it up later... I suppose you can borrow it if you find it useful :)

Using dynamic strings inside a regex can be annoying, especially if you can't guarantee that the strings are actually sane. Including a URL in a regular expression is downright obnoxious, since URLs and regular expressions share most of the same special characters, but they mean entirely different things. I threw this function together to make strings regexable. Enjoy.

Function

/**
 * Prepare a string for use in a regular expression.
 * 
 * @author Justin Hileman {@link http://justinhileman.com}
 * @access public
 * @param string $str
 * @return string
 */
function preg_real_escape_string($str) {
    $replace = array('\\' => '\\\\', '^' => '\^', '.' => '\.',
        '$' => '\$', '|' => '\|', '(' => '\(', ')' => '\)',
        '[' => '\[', ']' => '\]', '*' => '\*', '+' => '\+',
        '?' => '\?', '{' => '\{', '}' => '\}', ',' => '\,');
    return strtr($str, $replace);
}

Yeah, the name is a bit weird. This function is an analog to mysql_real_escape_string(), so it seemed fitting.

Usage

// basic usage (this is actually exactly the case I wrote this function for).
$regex = '/^' . preg_real_escape_string($name) . '/i';
$nameless = preg_replace($regex, '', $something_starting_with_name);
 
// trying to find a URL via regex can be especially obnoxious.
$regex = '/' . preg_real_escape_string($my_url) . '/';
$some_text = preg_replace($regex, '<a href="$1">$1</a>', $some_text);

My year in cities, 2009

I'm doing Kotke's cities thing. Here's my list for 2009:

  • Atlanta, Georgia1
  • Austin, Texas
  • Boston, Massachusettes
  • Denver, Colorado2
  • Los Angeles, California
  • Monteverde, Costa Rica
  • New Haven, Connecticut
  • New York, New York
  • Philadelphia, Pennsylvania
  • Portland, Oregon
  • Prosser, Washington
  • Provo, Utah
  • Puntarenas, Costa Rica
  • San Francisco, California
  • Washington DC
  • Wenatchee, Washington
  • Wilton, Connecticut

I spent one or more nights in every city on this list. Thanks to JetBlue's All You Can Jet pass for facilitating several of those trips :)


  1. Under duress. Thank you, Delta. 

  2. Also under duress, this time courtesy of United Airlines. 

Google Wave for iPhone is HOT.

Seriously. It even does realtime updates.

IMG_0293.PNG

IMG_0292.PNG

IMG_0285.PNG

IMG_0286.PNG

IMG_0287.PNG

IMG_0288.PNG

IMG_0289.PNG

IMG_0290.PNG

IMG_0291.PNG

Presented without comment

Strange Google keywords that landed at my blog

  • [searching for something dirty]
  • message link generator

Unrelated Yahoo search results that ended up here

  • 404 justin
  • 404 music / justin
  • acta "only one ipod"
  • base structure
  • best punchlines humor
  • blog [searching for something dirty]
  • cach a poo
  • cach the poo
  • clickable link generator
  • [searching for something dirty]
  • [searching for something dirty]
  • googol justin
  • how do you make fans
  • how much is the music industry worth
  • my space cool
  • [searching for something dirty]
  • [searching for something dirty]

A whole grip of Microsoft Live/MSN search terms which, in some crazy stretch of the imagination, might possibly relate to my blog

  • "types of stupidity"
  • +brain fard
  • +dot your happy friend dot info
  • +dot your space friend dot info
  • +what is acquaintances
  • add,message,comment links
  • again or contact info justin
  • alphebetize iphone menu
  • cach a poo
  • cach a poo 2
  • cach the poo
  • catch a poo
  • catchthe poo
  • chuck hileman blogger
  • clickable link generator
  • [searching for something dirty]
  • [searching for something dirty]
  • define musing
  • difference between a single dot ("./myimage.jpg") and a double dot ("../myimage.jpg")
  • dot your freind dot info
  • dot your happy friend dot info
  • dot your space friend dot info
  • frankie leman
  • google justin
  • googol hot open girls
  • hi my conputer info
  • hileman beer
  • [searching for something dirty]

And it appears that Bing might be carrying on the tradition

  • google justin
  • jastin for laught
  • [searching for something dirty]
  • "girl jeans"
  • advanced healing bandaid
  • arnold bennett
  • beautified quotes
  • bringo stop talking to machines
  • cach a poo
  • cartoon register groceries
  • doophp
  • dot unicode
  • download high def trailers
  • drupal webfm securing
  • em,px,
  • firefox really slowing down my internet
  • google.justin
  • gustaso.com
  • how to get firefox not to use memory when us load pages
  • how to save e-mail info on xp full install
  • howto disable url search ie
  • i think i have disabled number lock
  • ie6 backspace key
  • inspiration when sleepy

Context: Each list above consists of the unrelated search terms found in the top 50 keyword referrers from each search engine. For example, Google was wrong in about 4% of its top fifty search terms. Microsoft Live search failed in an astounding 76% of its tries.

For the curious: All the [searching for something dirty] placeholders were variations on the name of a certain website which will remain nameless.

Spyc vs. syck: Speedier YAML parsing in PHP

This post is mostly pretty pictures. I just ran some really quick benchmarks on a couple of of the YAML parsing options available for PHP. The blue line is the Spyc YAML library. The green line is the syck PECL extension. The yellow line is a standard PHP include(), and is on the chart as a reference point.

The first two charts were tested with a fixed file size and a variable number of iterations. The final chart tracks the change in parsing time as file size increases. Each test was run with 100 iterations, and the file size was doubled at each stage.

The pretty pictures

Average parse time

Total parse time

Total parse time for larger files

More info than you cared to know

  • This is a very informal benchmark. Feel free to supplement it with testing of your own.
  • All benchmarks were run on the same Ubuntu 9.04 virtual machine running PHP 5.2.x.
  • Tests were run in a Zoop Framework skeleton app (from the upcoming Lunar release), since I had one handy and it's really easy to deploy.
  • The YAML benchmarks on the first two charts consist of loading and parsing a ~160 line file (a YAML dump of the default Zoop skeleton configuration).
  • The PHP include benchmark exists only as a reference point. It consists of loading a var_dump()'d version of the same configuration array used in the YAML tests. This file was included via a standard PHP include().
  • The first two sets of tests were run with 1, 50, 100, 250, and 500 iterations, respectively. The final test was run with 100 iterations and variable file size.

In a classic piece, Joel takes on misconceptions about employee perks, programmer productivity and getting in "the flow". This is a great (re)read brought to you via commenter nomalab.