mod_rewrite & PHP: How to match urlencoded plus sign (+ = %2B)

I ran into trouble when trying to pass a urlencode()‘ed plus sign into a web address being processed by mod_rewrite.

$url = 'http://www.server.com/browsealpha/name+has+plus+in+it/';
$url = urlencode($url); // http://www.server.com/browsealpha/name%2Bhas%2Bplus%2Bin%2Bit/

This $url variable gets echo()‘d as a link in a page, so once it’s clicked and loaded in the browser, I then needed mod_rewrite to translate that to the actual URL, which is:

http://www.server.com/browsealpha.php?name=name%2Bhas%2Bplus%2Bin%2Bit

Here is the RewriteRule I was using to match:

# match any name containing any combination of letters, numbers, and the % sign (to match urlencoded URLs)
RewriteRule ^browsealpha/([%\w]*)/?$ /browsealpha.php?name=$1 [QSA,L]

This rule should match http://www.server.com/browsealpha/name%2Bhas%2Bplus%2Bin%2Bit/ but for some reason it wouldn’t work. After hours of frustration, I found a few threads mentioning the need to urlencode() the string twice, like so:

urlencode(urlencode($variable));

IT WORKS!!! Apparently this is because mod_rewrite automatically decodes the urlencoded URL, so if you pass in %2B, PHP sees it as %2B0. If you double encode, mod_rewrite decodes the first one, and PHP receives the second one (which is now %2B, which is what we want).

—————-
Now playing: Autechre – 444
via FoxyTunes

jQuery: How to set one <select> element to the value of another (using onchange equivalent)

Say you have an HTML <select> form element and you want to set another <select> element equal to the first one when it changes. Here’s an easy way to do so with jQuery:

$('#id_of_select1').change(function() {
    var select1_value = $(this).val();
    $('#id_of_select2').val(select1_value);
});

—————-
Now playing: Drexciya – Astronomical Guidepost
via FoxyTunes

jQuery: How to get the ID of an element.

Say you want to know the ID of an element using jQuery. You can achieve this easily using jQuery’s attr() method:

var currentId = $('#element').attr('id');

But this is fairly useless, because it requires you to already know the ID of the element that you want. Usually you’ll want to find out the ID if you don’t already know it — given a jQuery ‘this‘ object:

var currentId = $(this).attr('id');

This will only work provided that you have a valid jQuery object $(this) that you are working with, eg:

$(document).ready(function() {
  $('input.text').focus(function() {
    $('input.text').removeClass('onFocus'); /* remove focus state from all input elements */
    $(this).addClass('onFocus'); /* add focus state to currently clicked element */
    var currentId = $(this).attr('id');
  });
};

Using the code above, you will now know the ID of the currently focused input element. This can come in handy later on if you want to perform further actions on the element.

—————-
Now playing: Amon Tobin – Precursor (feat. Quadraceptor)
via FoxyTunes

Thunderbird HowTo: Show email count in folder pane.

If you’d like to see a count of how many emails are contained in each folder in Thunderbird, here’s how:

  1. Tools > Options > Advanced > General
  2. Check “Show expanded columns in the folder pane”
  3. Click OK to accept changes.
  4. At the top of the folder pane, click the column selection icon (Thunderbird Folder Icon) and select the columns that you would like to see. You can choose to display Total, Unread, and/or Size.

—————-
Now playing: Physics – First 7″ – Side 2
via FoxyTunes

iPod stuck with Apple logo on screen

My 3G iPod got stuck with the Apple logo on its screen. The way to fix this is to put the iPod into Disk Mode and restore it from there. Here is how:

  1. Reset the iPod by holding down the Menu and Play buttons simultaneously.
  2. As soon as it reboots, hold down the Fast Forward and Rewind buttons simultaneously.
  3. Your iPod will now be in “Disk Mode” and you should be able to plug it into your computer, open up iTunes, and restore your iPod.

iPod does not appear in iTunes.

I have an old 3G iPod, and all of a sudden it would not show up in iTunes when connected to my computer via Firewire or USB. It did show up in Finder. After some research, the only way I found to fix this is to format it using Apple’s Disk Utility. WARNING: Doing this will wipe everything off your iPod. You won’t be able to recover the files off your iPod, but at least you’ll be able to use it with iTunes again.

  1. Connect your iPod you to your Mac.
  2. Open up Disk Utility (Finder > Applications > Utilities > Disk Utility).
  3. Select your iPod in the left-hand window pane.
  4. Click the “Erase” tab, accept all defaults, then click the “Erase” button.

Your iPod will now be re-formatted, and you should be able to plug it into iTunes and Restore the updated iPod software.

UPDATE: For PCs, you may be able to do something similar with Windows’ Disk Manager. I haven’t tried though, so I can’t verify, but I bet it would work on Windows too.

Max OS X Leopard – Disappointed (but not how you think)

“New Macs will come with Leopard Pre-Installed…” – Apple’s Leopard Guided Tour Video

So today is the release of Apple’s next generation version of OS X: Leopard. I had planned on purchasing an iMac a couple months ago, but decided to delay my purchase until the release of Leopard. So I go down to the Apple store (which was PACKED) and proceeded to purchase a shiny new iMac. Excited to try out Leopard, I rip it out of the box and turn it on. And to my disappointment, it boots into Tiger. WTF!? I delayed buying a new iMac for Leopard and I get… TIGER?!!? Turns out Leopard isn’t pre-installed on my iMac. Apple simply threw a Leopard DVD into the box expecting me to sit through a manual upgrade. At the end of the day I don’t really care, but here I am ranting on my blog (from my Windows machine) instead of playing with my brand new Leopard-powered iMac. Ugh!

I always thought that Macs were supposed to just work! What’s with all this manual tinkering and upgrading and waiting and slowly-moving progress bar watching? For a minute there I thought I had bought a new PC running Windows… Shame on you Apple.