ASCII Quotes to Typographical Quotes
ASCII quotes are what you see when you work with plain text software (not unicode text). Something like this:
He said, "You've got 'spicies' in your pie."
Typographical quotes are sometimes called educated quotes or curly quotes. The ASCII quotes in the above example converted to typographical quotes looks like this:
He said, “You’ve got ‘spicies’ in your pie.”
Notice that the ASCII apostrophe is also converted. It became a typographical apostrophe.
This article contains a PHP function to change ASCII quotes to HTML entities for typographical quotes.
• An ASCII double-quote (") is converted to either “ or ” (which publish as “ or ”).
• An ASCII single-quote/apostrophe (') is converted to either ‘ or ’ (which publish as ‘ or ’).
Here is the function:
function ASCIIquotes2Typograpicals($s)
{
$s = preg_replace('/(\S)"/','$1”',$s);
$s = preg_replace('/"/','“',$s);
$s = preg_replace("/(\S)'/",'$1’',$s);
$s = preg_replace("/'/",'‘',$s);
return $s;
}
The ASCIIquotes2Typograpicals() function scans the text it is given and converts ASCII quotes into HTML entities for publishing typographical quotes.
Note: When converting text, don't include HTML tags with id or class information. The ASCII quotes witin the HTML tags would get converted, too, which is generally unwanted.
Here is a quick little demonstration program that uses the ASCIIquotes2Typograpicals() function.
<?php
$text = <<<LINE
He said, "You've got 'spicies' in your pie."
LINE;
$text = ASCIIquotes2Typograpicals($text);
echo $text;
function ASCIIquotes2Typograpicals($s)
{
$s = preg_replace('/(\S)"/','$1”',$s);
$s = preg_replace('/"/','“',$s);
$s = preg_replace("/(\S)'/",'$1’',$s);
$s = preg_replace("/'/",'‘',$s);
return $s;
}
?>
Us programmer types generally use ASCII text processors for creating and editing code. Because we're used to that software, we use it for other things, too, like web page content.
As an example of real use, let's suppose you maintain a section of a web page as a separate file. It's easier to update that way and can easily be pulled into the web page with a line of PHP code. Something like this:
<?php echo(file_get_contents('my_file.txt')) ?>
If you use ASCII quotes in that text (and no HTML tags that have quotation marks), you can filter it through the ASCIIquotes2Typograpicals() function. Put the function anywhere in the source code of the web page and then use this line.
<?php echo(ASCIIquotes2Typograpicals(file_get_contents('my_file.txt'))) ?>
Voilà, ASCII quotes now appear as typographical quotes.
(This article first appeared with an issue of the Possibilities newsletter.)
Will Bontrager

