burger menu icon
WillMaster

WillMasterBlog > PHP

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

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&rdquo;',$s);
   $s = preg_replace('/"/','&ldquo;',$s);
   $s = preg_replace("/(\S)'/",'$1&rsquo;',$s);
   $s = preg_replace("/'/",'&lsquo;',$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

Was this blog post helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method


All information in WillMaster Blog articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

Recent Articles in the Library

Avoid Expired SSL Certificate

Check the expiration date of your domain's SSL certificate

Image Zoom on Hover

Zoom the image with a hover. CSS. No JavaScript.

The INS and DEL Tags

The INS and DEL tags come in mighty handy to display editing marks and to emphasize ideas with HTML markup.

Viewing HTML Source Code

Three ways to view HTML source code.

The HTML address Tag

The address tag is used to present an address to the site user. It also makes the address available to website search engines.

Rotate

The CSS 'rotate' property is a versatile way to rotate elements.

Building a URL to Self With PHP

PHP has all the parts to build the URL it was accessed with.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2026 Will Bontrager Software LLC