Tim's Weblog
Tim Strehle’s links and thoughts on Web apps, software development and Digital Asset Management, since 2002.
2013-02-26

Non-breaking space accidentally typed in OS X kills PHP code? Use Karabiner

I finally found a solution to a problem that has bugged me for years:

When editing PHP source code on my Mac, the code would occasionally break completely with an error message like “PHP Fatal error: Cannot use [] for reading in … on line …”. The code on that line would look completely fine in the editor, say: “$a[] = 1;”

Only when I’d switch on “Show Invisibles” in TextWrangler, I’d see some funny bullet in front of the equals sign. What looked like a regular space character was actually a non-breaking space (Unicode U+00A0, officially called “No-Break Space”, “c2 a0” in a UTF-8 file’s hexdump).

It was inserted because I typed to fast (or too sloppily); I had been hitting the space bar while the Alt key was still pressed. (This wouldn’t happen on a standard US keyboard, but the German keyboard layout requires the Alt key for typing square [ ] and curly brackets { }.)

The superuser article How to disable the Option-Space key combination for non-breaking spaces? had the solution: Install the free Karabiner (formerly named KeyRemap4MacBook) (requires a reboot), search for “option+space” in its preferences and check the “Non-Breaking Space to Normal Space” option.

Update: KeyRemap4MacBook has been renamed to Karabiner. And here’s a PHP script to find non-breaking spaces in your source code.