Textwrangler Tips – Compare files, keyboard shortcuts

A few quick TextWrangler tips.

I never knew until recently that you can compare files in TextWrangler… and its easy.

Simply highlight the 2 files you want to compare in the Documents drawer, right click on them and select the option ‘Compare Selected Documents‘.

Now the 2 files will open into 2 windows with a review of the differences below them. You can then apply the differences in either direction.

Another tip is adding keyboard shortcuts to TextWrangler’s menu options.
Read more

JavaScript Tricks; URLEncode, Foreach and Remove last character

I have come across some neat (if not mind blowing) tricks with JavaScript recently.

Firstly, I needed JavaScript code that copied PHP’s urlencode().
This function returns an encoded string where all non-alphanumeric characters except - _ . are replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs.

JavaScript has 2 functions to emulate urlencode(), but both fall short…
The escape function encodes all non-alphanumeric characters except * @ / + – _ .
The encodeURIComponent function encodes all non-alphanumeric characters except spaces and ! ‘ ( ) * ~

The winner for me though is encodeURIComponent as it encodes all UTF-8 characters while escape only encodes ISO Latin characters.

With this in hand, mozilla help provide a neat function.
Read more

Post XML with HTTPS Authentication using PHP and cURL

Recently I needed to post XML to a particular API and this API required HTTPS authentication. So I needed to send a username / password with the request.

This was pretty straightforward in C#.NET ( at least from my foggy memory ). You needed to use NetworkCredentials. But it was a bit difficult to find a similar way of doing this in PHP… but eventually I found this gem.

This uses cURL which is just a library to send and receive data from remote sites. Now all I needed was a neat way of using cURL to post XML… and I found a neat implementation here.

Two plus two and you get …
Read more

Auto highlight text inside pre tags using jQuery

We use pre tags in the Polldaddy plugin to display the embed codes and short URLs. We use pre tags because it formats the text inside the tags as we insert it, which is ideal for displaying code in particular. The thing is, people like to copy this code and they have to go to the awful bother of selecting all the text manually if they wish to copy the code or URL.

The alternative is to use an text input or text area which will allow you to use a select method that you can call on a click event, but then you lose the neat formatting that pre tags offer.

So I had a look for a jQuery hack to auto select all the text inside the pre tags. The only complication with this appears to be a cross platform solution as each browser appears to have their own way of selecting text.
Read more

Revert or rollback a SVN revision

If you have ever deployed code from a SVN repository with a bug in it and you need to revert to a working revision then you may find the following handy.

First find the revision numbers for the file(s) in question.

Use the log command to get the info from previous commits including revision numbers.

So something like… Read more

JavaScript, ExternalInterface, Flash and Local Shared Objects

I was looking into flash’s local shared objects ( from here on LSO’s ) recently as a method of data persistence on a clients browser as the plain old HTTP cookie has its limitations.

LSO’s are like cookies and are sometimes referred to flash cookies but there are 2 main differences between them. A normal HTTP cookie can store around 4k of data, while the flash cookie can store up to 100k. Also a normal cookie is pretty easy to remove while removing a flash cookie is far more convoluted, which is a matter of some contention.
Read more

Regular Expressions

First off, regular expressions are great.

They are a handy quick way to validate or parse data and you can use them in almost all languages. But of all things, I forget neat regex’s and in fairness they are a pita to recall as the syntax is plain nutty.

This is where it ends. I am going to reference all the neat regex’s in this blog as I come across them rather than rely on mother Google.
Read more

Microsoft SQL Service Broker

What is Microsoft’s SQL service broker?
Its a message queuing platform on Microsoft’s SQL Server. It allows you to queue up jobs for the database to deal with asynchronously.

Why use it?
I’m no DBA, but in my case, I was working with a database server that was at full tilt and there was work that it could be handle asynchronously to spread the load. So there was data that needed processing but it did not need to be handled immediately, like data for some reports. This data could be placed on a queue to be processed by the database server in it’s own good time, evening out the demands on the server.

How does it work?
Well its pretty straightforward yet it looks a bit ridiculous written down. I think this because as a platform it can handle a lot of data messaging scenario’s and as such the syntax and examples usually given are quite generic.

Here’s the official guide, happy mining through all that!

You can find better ones here and here.

Now for my explanation… Read more

Odd or Even Number

I love gems like this. I come across them from time to time but they never stick… then I do a search to find them again.

Anyway a simple way to determine if a number is odd or even, use the following code.

function is_odd( $number ) {
    return $number & 1;
}

Quick explanation
& is a binary AND.

If you have a binary value, and you AND with another binary value, then the result will be the bitwise AND of the two.

Binary values are a series of bits or 1′s and 0′s. However we are only concerned with the rightmost one ( the least significant bit ) as when a binary value ends with a 1, it is odd… when it ends with a 0 it is even.

Now, if I pass that function say the number 13, the binary value will look like 1101.
When you AND with a 1, or binary value 0001, it looks like this…

1101 & 0001 = 0001

We look to the least significant bit, which is a 1, which means the number is odd.

A simple explanation of binary can be found here.

Format XML with TextWrangler

My text editor of choice on the Mac is TextWrangler. It’s lightweight and it has pretty much all you need from a text editor. In particular, I like that I can SFTP into my development server.

One issue that bugged me lately was when I opened an unindented, unformatted XML file. Basically, it looked a mess and there was no way to tidy the file up so that I could read it easily.

However, I found a simple way to do this today… Read more