Smart Quotes in PHP

One of the true joys I find in reading different websites is when the author of whatever text you see has taken the time to make his text typographically pleasing to the eye through the use of proper typographical elements. CSS has enabled designers to shape text on the web in ways that allow for far greater control over presentation than the creators of HTML ever envisioned. However, I see many sites where it’s obvious that great pain has been put into the layout and presentation of the text, but there are still things like single and double prime marks being used instead of true quotes or apostrophes. Part of the reason for this is it’s a pain to enter the proper entities in when you type, especially if the entry is being added through a normal text box like most blogging software use. While I’m not going to go start a society (more) I still have written a small function in PHP that will hopefully make the world a better place, one curly quote at a time :). Thanks to my dad, Mark Pilgrim for inspiration and the code that got me started, and Barrett for help.

A little background: This whole thing started a few hours ago when I was writing a paper and when I looked back to proofread I saw that there were a number of occurrences of words like it’s, where writing out the HTML entities had become so ingrained in me from various situations where I hand code that it was now translating into my ‘normal’ typing. At that moment I immediately thought of ten other reasons why it’s probably better for the content to be entered into the database as a single or double prime and then translated to its proper character on display. Most of all, it’s just easier, and the free flow of ideas into your writing is not impeded in any way. My mind also went back to an entry I read on Dive Into Mark early last month which addressed a similar issue, but from looking at the code I saw no easy way I could drop that into my site. And thus this very generic function was born. It can be dropped into any PHP application anytime you want to make some text display worthy. Without further ado: Update: fixed a display issue, and a small bug.


Okay, a little more ado :). I’m posting instructions for b2, greymatter, and MT users tomorrow (actually later today), but until then feel free to gank the regex part of this and put it whereever you’d like.

This code has been deprecated. Please use the newer version.

Let me know if you run into any problems. I am very open to suggestions as to making this faster and/or more efficient. I actually just learned regular expressions, well, tonight, so I’m by no means proposing this as the single best way to do this.

6 thoughts on “Smart Quotes in PHP

  1. this is what i read….
    “blah blah blah text on the web blah blah blah. However, I see many sites where it’s obvious that great pain has been put into the blah blah blah. While I’m not going to go start a society, I still have written a small blah blah blah make the world a better place, one blah blah blah. Thanks to Mark Pilgrim for inspiration and the blah blah blah blah.
    just kidding, matt. you know i love ya;)

SHARE YOUR THOUGHTS