January, 2008


25
Jan 08

natsort() pain in PHP

While writing some software for work which works out distances between geographic locations, I discovered some nastiness with the natsort() function in php.

This function implements a sort algorithm that orders alphanumeric strings in the way a human being would while maintaining key/value associations. This is described as a “natural ordering”.

So, given this definition of natsort, I’ll now outline a working example and then an example of the problem I encountered.
Lets say we have a list of values in an array that need to be sorted descending whilst maintaining key/value pairs. E.g:

$a = array(4 => 9.1, 3 => 4.2, 10 => 4.5);
natsort($a);

As you would expect, the resulting array looks a little something like this.

print_r($a);
Array
(
[3] => 4.2
[10] => 4.5
[4] => 9.1
)

But what happens when the values in the array we are trying to sort are a little more complex:

$a = array(4 => 9.11705244995,3 => 4.23956628237,10 => 4.5361796771);
natsort($a);

The resulting array isn’t what we expect though.

print_r($a);
Array
(
[10] => 4.5361796771
[3] => 4.23956628237
[4] => 9.11705244995
)

As you can see, the sort order is incorrect. As you have probably picked up, the 4.5xxx number’s decimal places are 1 less than the other two numbers. As soon as you change 4.5361796771 to 4.53617967711 and run natsort() over it, it returns the correct order.

Conclusion: Don’t use natsort() when sorting floats of different precision as it will return incorrect values! Use something like asort() if you want to maintain key/value pairs, or sort() if you don’t care about the key ordering.


9
Jan 08

APML2JSON Script/Service

This script takes an APML feed, and parses it into valid APML-JSON based on the APML-JSON spec on the APML wiki. Instead of manually parsing the APML into JSON, I have used the XSLT file attached to the aforementioned spec page along with xsltproc to generate the JSON data. The idea behind this script is based on John Resig’s RSS2JSON script.At the moment the script is pretty hacky for release, so I have provided a REST interface that can be accessed via a GET request.

A request to the interface would take the following form:
http://bmn.name/examples/apml2json/?url=URL&callback=CALLBACK

The callback parameter is optional. If specified, the resulting JSON is wrapped in the callback for easy parsing at the client end, otherwise the resulting JS Object is assigned to a variable which can be accessed via JS. The results from the call are cached hourly to reduce the load on the server. :)

Example Interface call: http://bmn.name/examples/apml2json/?
url=http://blog.bmn.name/index.php?apml=apml&callback=parseFeed

For more information on how to use the resulting JS, head over to John’s RSS2JSON page as he provides some sample JS.


5
Jan 08

Blu-ray PS3 stuttering playback woes… Solved!

This blog post is for anyone who owns a PS3 and has discovered that *some* Blu-ray discs have stuttering/dropped frames playback.

I purchased a Playstation 3 back in April 2007, and received a free copy of Casino Royale on Blu-ray as part of a PS3 promotion. Needless to say, I opened the PS3 and stuck the blu-ray disc in to the drive to see what all the blu-ray fuss was about. The movie looked fantastic, even on my standard def TV. It looked even better on my brothers HDTV. Now you may be wondering why I’m saying this. Well, as I later found out, this initial experience was not indicative of the PS3’s blu-ray performance on every blu-ray disc.

Fast-forward ahead 8 months.

I decided to go halves in a brand new Samsung Full HDTV (1080p) for christmas. The picture quality of this baby is phenomenal on both HD FTA and PS3. As a present to myself, I went to JB-HIFI and purchased 3 blu-ray movies to try on the brand new tv. I bought Kingdom of Heaven, Gone in 60 Seconds and Behind Enemy Lines; all of which are fantastic movies. I tried out Behind Enemy Lines, and it looked spectacular. I then tried Kingdom of Heaven and had a similar reaction. Then, as you would have guessed, I tried Gone in 60 Seconds…

Arrrgghhhh! What the hell did they do to make this movie so painful to watch? The movie started out fine, but after about 40 seconds, the video started to stutter. It appeared as though video frames were being dropped as the audio wasn’t affected.

I continued to watch once the stuttering had stopped to see if it was a once off. But I was disappointed to find out that it continued to do it every 1 minute or so for about 10 seconds. I became very frustrated as blu-ray discs aren’t cheap. So I went back to JB-HIFI and swapped the disc for another copy of Gone in 60 Seconds so I could work out whether it was a disc problem or a PS3 problem.

I slid the disc into the console and held my breath…

Right on queue, it started to stutter. Argh! At this point I was very angry. I decided to have a hunt around in the BD/DVD settings section of the PS3. One option I found seemed like it fit the bill.

“BD 1080p 24Hz Output (HDMI) – Sets the playback method for BDs recorded at 24Hz (frames/second)”

By default this option is set to ‘Automatic’, so I decided to fiddle with this setting and set it to ‘Off’. I started the blu-ray disc up again and held my breath…

40 seconds passed…. 50 seconds passed… 5 minutes passed…

Nothing! Not a single stutter or anything! The problem was fixed. There is nothing on the blu-ray disc cover to suggest that the movie is in 24fps, so I guess its trial and error for each disc. But at least you now know how to fix the problem!

I’d like to hear from other people who have had the same problem.

EDIT: I have found a few resources on the net of other people experiencing the same problem, so if you wish to know the exact reason for this stuttering – read this thread.


5
Jan 08

Photography… missing it badly

Over the last few weeks, I have been spending a larger than usual portion of my time browsing Flickr and other photography websites as a change from programming. I had forgotten how much I love going out with my 35mm Canon EOS SLR and spending hours composing photos. I am a big fan of traditional photography and refuse to use DSLR camera’s for photography as I feel photographers no longer spend the time composing the photo, but rather, snap a huge number of photos and pick the best one.

Expect to see more blog posts about photography and snaps I take.

In the meantime, here is a fantastic photo I found browsing Flickr. It is entitled ‘Nightdrive’ and is taken by c.zwerg in Tyrol. Check out the photo’s Flickr page here.

Nightdrive