BBEdit: Escape and Encase a String in Quotes (Updated)
2007-01-28: I’ve updated the script to correct for a stupid mistake on my part. Now single quotes won’t be unnecessarily escaped.
Here at the Firefall Pro labs we use BBEdit for most of what we do, which is programming of some sort. While working on a PHP script it occurred to me that I spend a small portion of my time encasing strings in quotes and escaping quotes within. Normally this is insignificant, but this time quickly adds up when encapsulating bits of html markup.
The typical recourse is to run a find and replace on selected text and have the quotes replaced with a backslash and a quote (\”). However this only accounts for one type of quotes. To get both you either need to run it again, or make a GREP pattern. Often either solution ends up taking almost the same amount of time that it would to do it by hand, albeit with a little more accuracy, but not at a huge savings in time.
I had hoped that BBEdit would magically have some sort of built in function to do this. They have a few text transformation tools, why not this? Certainly everyone who programs in BBEdit would use it. I didn’t find it, so I e-mailed it in as a feature request. They said maybe it would find it’s way in one day, but till then I should just write my own.
So I did. This file should be placed in BBEdit’s Unix Filters folder and the “.txt” ending should be changed to “.php”. It should run in most BBEdit 8 / 10.4 environments. Naturally use it at your own risk. Despite how simplistic it is, if you find it useful, let me know.