Library: for developers
- PHP SmartyPants Lib 1.7.1 (16 Kb)
- Library package for developers. Requires PHP 5.3. Also available on Packagist et and Github. October 10, 2016.
Classic Version: plugin for WordPress, Smarty, etc.
- Classic version → PHP SmartyPants & PHP SmartyPants Typographer
- Important: classic version of PHP SmartyPants and PHP SmartyPants Typographer is no longer being supported. If you’re a developer using PHP 5.3 or later, you should probably use the Lib version above.
This is a library package that includes the PHP SmartyPants and its sibling PHP SmartyPants Typographer with additional features.
SmartyPants is a free web typography prettifyier tool for web writers. It easily translates plain ASCII punctuation characters into “smart” typographic punctuation HTML entities.
PHP SmartyPants is a port to PHP of the original SmartyPants written in Perl by John Gruber.
SmartyPants can perform the following transformations:
- Straight quotes (
') into “curly” quote HTML entities
- Backtick-style quotes (
``like this'') into “curly” quote HTML entities
- Dashes (
---) into en- and em-dash entities
- Three consecutive dots (
...) into an ellipsis entity
SmartyPants Typographer can perform additional transformations:
- French guillemets done using (
>>) into true « guillemets » HTML entities.
- Comma-style quotes (
''like this,,) into their curly equivalent.
- Replace existing spaces with non-break spaces around punctuation marks where appropriate, can also add or remove them if configured to.
- Replace existing spaces with non-break spaces for spaces used as a thousand separator and between a number and the unit symbol that follows it (for most common units).
This means you can write, edit, and save using plain old ASCII straight quotes, plain dashes, and plain dots, but your published posts (and final HTML output) will appear with smart quotes, em-dashes, proper ellipses, and proper no-break spaces (with Typographer).
SmartyPants does not modify characters within
<script> tag blocks. Typically, these tags are used to
display text where smart quotes and other “smart punctuation” would not
be appropriate, such as source code or example markup.
If you need to use literal straight quotes (or plain hyphens and periods), SmartyPants accepts the following backslash escape sequences to force non-smart punctuation. It does so by transforming the escape sequence into a decimal-encoded HTML entity:
This is useful, for example, when you want to use straight quotes as foot and inch marks:
in SmartyPants’s HTML output. Which, when rendered by a web browser, looks like:
Installation and Requirement
This library package requires PHP 5.3 or later.
Note: The older plugin/library hybrid package for PHP SmartyPants and PHP SmartyPants Typographer is still will work with PHP 4.0.5 and later.
This library package is meant to be used with class autoloading. For autoloading
to work, your project needs have setup a PSR-0-compatible autoloader. See the
included Readme.php file for a minimal autoloader setup. (If you don’t want to
use autoloading you can do a classic
require_once to manually include the
files prior use instead.)
With class autoloading in place, putting the ‘Michelf’ folder in your include path should be enough for this to work:
use \Michelf\SmartyPants; $html_output = SmartyPants::defaultTransform($html_input);
SmartyPants Typographer is also available the same way:
use \Michelf\SmartyPantsTypographer; $html_output = SmartyPantsTypographer::defaultTransform($html_input);
If you are using PHP SmartyPants with another text filter function that generates HTML such as Markdown, you should filter the text after the HTML-generating filter. This is an example with PHP Markdown:
use \Michelf\Markdown, \Michelf\SmartyPants; $my_html = Markdown::defaultTransform($my_text); $my_html = SmartyPants::defaultTransform($my_html);
To learn more about configuration options, see the full list of configuration variables.
Usage Without an Autoloader
If you cannot use class autoloading, you can still use include or require to access the parser. To load the \Michelf\SmartyPants parser, do it this way:
Or, if you need the \Michelf\SmartyPantsTypographer parser:
While the plain
.php files depend on autoloading to work correctly, using the
.inc.php files instead will eagerly load the dependencies that would be loaded
on demand if you were using autoloading.
One situation in which quotes will get curled the wrong way is when apostrophes are used at the start of leading contractions. For example:
'Twas the night before Christmas.
In the case above, SmartyPants will turn the apostrophe into an opening
single-quote, when in fact it should be a closing one. I don’t think
this problem can be solved in the general case — every word processor
I’ve tried gets this wrong as well. In such cases, it’s best to use the
proper HTML entity for closing single-quotes (
To file bug reports or feature requests (other than topics listed in the Caveats section above) please send email to:
If the bug involves quotes being curled the wrong way, please send example text to illustrate.
Copyright and License
Copyright (c) 2005-2016 Michel Fortin https://michelf.ca/ All rights reserved.
Copyright (c) 2003-2004 John Gruber https://daringfireball.net/ All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name “SmartyPants” nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.