This is old content! The graveyard is a snapshot of content created from 2002-2005. For new stuff, visit Maniacal Rage.
 

Curly Quotes

posted on december 7, 2002, tag: site

Shawn managed to get me interested in changing all quotes in the content on this site to the proper curly quotes rather than plain quotes ("), as well as single quotes. It didn't take too long to find a function already written by Matthew Mullenweg. This function worked well for PHP, and with some simple tweaks, I created a function that will do exactly what both Shawn and I wanted. If you're using PHP, feel free to use the function below (note that due to content spacing here, lines wrap... but if you copy and paste this code, it will be formatted correctly as far as spacing is concerned):

function prettyUp($ugly) {
     // Credit due to Matthew Mullenweg (www.photomatt.net)
     // First do ampersands, as the following regexes will need real ones
     $ugly = str_replace('&',"&",$ugly);
     // Now make single quotes pretty
     $ugly = preg_replace("/'([dmst])([ .,?!)\/<])/i","&#8217;$1$2",$ugly);
     $ugly = preg_replace("/'([lrv])([el])([ .,?!)\/<])/i","&#8217;$1$2$3",$ugly);
     $ugly = preg_replace("/([^=])'([^ >].*)'/U","$1&#8216;$2&#8217;",$ugly);
     // Double quotes now
     $ugly = preg_replace('/([^=])"([^ >].*)"/U',"$1&#8220;$2&#8221;",$ugly);
     // If the quotes span Ps, make them pretty too
     $ugly = preg_replace('/<p>"(.*)<\/p>/U',"<p>&#8220;$1</p>",$ugly);
     // True EM dashes
     $ugly = str_replace('--',"&#151;",$ugly);
     // All done
     return $ugly;
}

To use this, simply run the function on your content just before using it, as such:

// Assuming your content is in the $content variable
$content = prettyUp($content);
// Then you can echo or print to your heart's content
echo $content;

If you're a Movable Type user, as I am, you should check out John Gruber's excellent plugin for MT, called SmartyPants. It works perfectly. Rage is using a combination of both and is now a little bit prettier.

Comments

There are 8 comments, comments are closed

Linus on 12/07/2002:

Always determined to re-invent the wheel, this is what I'd do if I wanted to have nice opening and closing quotes on my site: wrap things I want quotes in and or something, then feed the content to an output buffer, and run a simple str_replace on it. Of course, with Matthew Mullenweg's way you can use the function on old content, too...

Linus on 12/07/2002:

Hmm. This comment system doesn't seem to neuter HTML. What I meant to say up there is that I'd wrap thing I wanted quoted in <quo> and </quo> tags.

Garrett on 12/08/2002:

Doesn't it seem like a lot more work to write <quo> each time rather than just using a simple function that does it automatically?

Linus on 12/08/2002:

Maybe, but it'd really just become like any other HTML tag, like enclosing paragraphs in <p> tags instead of just a couple of returns between each one...

Garrett on 12/08/2002:

Right, but I think the point is still that this makes life easier... rather than forcing you to do something new, this allows you to do everything the way you used to, only it will automatically make it prettier.

Linus on 12/09/2002:

Psst. My response was actually a result of my mind being blown by most of the regexs in that function. :)

photomatt on 12/12/2002:

If you do a simple str_replace for the easy stuff you'll notice a nice speed pickup. The regex functions are incredibly slow when compared to the string ones.

Garrett on 12/21/2002:

Absolutely good point. Didn't think about it at the time. Function updated. Thanks again.


See Also

View the archive

Original iPod Introduction
How far we've come in just a few short years. Here's where it all started.

Front Row on Non-iMacs
Going to try this tonight!

WriAShorStorWe!
DY starts a one-week short story writing event for people to lazy to enty NaNoWriMo. VerCooIdea.

Lost Rhapsody
Funny Flash movie using Weird Al music and Lost stuff. Lyrics make a surprising amount of sense!

Jed's Other Poem
Unsolicited music video made on an Apple ][. Fantastic!

Printers Output Secret Barcode
The government is keeping tabs on what you print, with the help of major printer companies.

Dreamhost Promo Codes
DH already has very cheap, very good hosting—this just sweetens the deal.

Photos of the new iPod
Just received my new iPod and I put a few photos up.

PEZ MP3 Player
Funny idea that actually looks kind of neat. I like that it comes pre-loaded with "indie" music.

HD Easter Egg
"My Name is Earl" on NBC gives viewers with HD TVs a little easter egg. Cute, but weird.