Features
PHP Typography is a PHP based solution to greatly improve web typography. It features the following capabilities (including granular control):
- Hyphenation
- Spacing control, including: gluing values to units, widow protection, and forced internal wrapping of long URLs & email addresses.
- Intelligent character replacement, including smart handling of: quote marks ( “foo” ), dashes ( foo – bar ), ellipses ( … ), trademarks ( ™ ), math symbols ( 1024×768 ), fractions ( 12⁄23 ), and ordinal suffixes ( 3rd )
- CSS hooks for styling: ampersands (class “amp”), acronyms (class “caps”), numbers (class “numbers”), initial single quotes (class “quo”), and initial double quotes & guillemets (class “dquo”).
This project merges, builds upon, and generalizes the functionality of wp-Typogrify, wp-Hyphenate and SmartyPants.
Implementation
While complication is added when manipulating the many options, implementing PHP Typography can be as easy as this:
<?php
$html = "raw HTML here... ";
include('path/to/php-typography.php');
$typo = new phpTypography();
$html = $typo->process($html);
echo $html;
?>
PHP Typography is designed for easy integration with plugin architectures of PHP based content management systems. It has already been ported to WordPress and Zikula CMS. We would like to see other developers pick up this code and implement it for other CMSs. Specifically:
- Drupal
- Joomla
- Expression Engine
If you are a developer for these (or any other PHP based CMS), the heavy lifting is done. You just need to hook it into your CMS.
Testimonial
Here is a fine testimonial from Jenny (who also provided some valuable feedback to further improve the project):
I’ve been evaluating PHP Typography for use in a custom CMS and have been very pleased so far…. It was, as you promised, easy to integrate in our framework (ZF) by wrapping it in a simple class implementing Zend_Filter_Interface. I am using PHP Typography after running the text through HTMLPurifier, and the output has been beautiful thus far.
Requirements
PHP Typography has the following requirements:
- the host server must run PHP 5 or later
- text must be encoded UTF-8
- all markup must be valid xHTML, specifically:
- every element must be closed,
- every attribute must have a value enclosed in quotes, and
- tag names and attributes must be lowercase.
Related
It should also be noted, PHP Typography would not have been possible without another tool we have recently released: PHP Parser. PHP Parser is an amazingly powerful resource that allows HTML to be parsed, modified and reconstructed in ways previously not possible.
Your feedback is much appreciated. How can we make this plugin better? Have you developed a plugin using PHP Typography? Email us at info@kingdesk.com


Jeffrey D. King • 11:30 am on Jul. 9, 2009
A commenter on the wp-Typography project notified us a false positive was returned when the wp-Typography project files were scanned by Avira anti-virus. This issue appears to also occur with the PHP-Typography project.
We have confirmed false positives for the
cy.php,en-GB.php,es.php,ga.php,hr.php, andpr.phplanguage files. We have been unable to divine the cause. We have reported the false positives to Avira and provided them with a copy of the files.Avira’s review of the faslse positives may be viewed here.
We have also tested the files in AVG, and are happy to report no false positives with their anti-virus solution.
Beli • 9:48 am on Jul. 22, 2009
From your article I definitely think that PHP typography sounds great. I will download it, try it and will contact you after that.
Webdesign Stuttgart
Stephen Coles • 3:56 pm on Jul. 24, 2009
Does the hyphenation provide an exception for capitalized words? I love the Hyphenate plugin, but protecting proper nouns is essential for many publications, so I can’t use it if I can’t filter capitalized words.
Jeffrey D. King • 5:07 pm on Jul. 27, 2009
@Stephen Coles #24
A new method
set_hyphenate_title_case()was added to version 1.5 to meet your need.Daniel • 4:02 pm on Jul. 29, 2009
The new WP-MU beta plugin is great news. I tried it out, and this time it actually works under MU as well. Or at least, I can change the hyphenation language this time…
:-)
BTW: A terrific feature would be if the default hyphenation language would be the same as the blog language. Even more so on a MU site, when I activate that plugin for all blogs. And how would you deal with a multilanguage blog? Most of the blogs on my MU install are german, some of them english, and a few are multi-language (german, english, french, maybe italian, take your choice — the last three are official languages within Switzerland), where the user can switch to his favorite language on-the-fly.
Thanks a lot for a wonderful plugin!
– Daniel
Dirk Hillbrecht • 3:15 am on Aug. 2, 2009
Hi,
the typography plugin is really great. It just happens to make a mistake with dates written in ISO format, e.g. “2009 – 08-02″. In these, the first hyphen is enlarged, the second is not. The whole date looks quite garbled. Perhaps you can fix that in some new release? Apart from this, the produced output is really nice and improves the “usage experience” of “the average blog” quite considerably. Thank you!
Best regards,
Dirk
Jeffrey D. King • 2:04 pm on Aug. 4, 2009
@Dirk Hillbrecht #5
Thank you for bringing this issue to my attention. It has been corrected in version 1.8
Dirk Hillbrecht • 10:20 pm on Aug. 4, 2009
…just installed 1.8 and date formatting now works marvellous. Thanks again for this great piece of software! Dirk
Priit Pirita • 6:15 am on Nov. 20, 2009
There seems to be bug in style_initial_quotes(). All 3 mb_substr() there are without encoding parameter. Only after adding “UTF-8″ it works.
Bill • 5:25 pm on Dec. 1, 2009
While integrating this with my project (running on Kohana VO3) I ran into some problems. It works great until it finds any tags, classes, or IDs set to ignore. I’m not sure what the problem is, but when I try to add anything that is listed in the settings[“ignoreTags”], settings[“ignoreClasses”], or settings[“ignoreIDs”] arrays of the phpTypography class, I get an error that says the following:
ErrorException [ Notice ]: Undefined index: openPos.
Jeffrey D. King • 9:48 pm on Dec. 1, 2009
@Priit Pirita #8 & Bill # 9,
Both your issues should be resolved in the new version 1.19 release.
Andrew Hutchings • 3:00 pm on Dec. 18, 2009
I found a bug in version 1.19: on line 1950, $encodings is used without being initialized. Otherwise, works great!
Erik Spaan • 12:08 am on Dec. 22, 2009
Hi,
The plugin has been made available as a plugni to the Zikula CMS by Steffen Voß, check out http://community.zikula.org/module-Extensions-display-ot-component-componentid-264.htm for the details.
Jeffrey D. King • 8:41 am on Dec. 22, 2009
@Erik Spann #12
Thanks for pointing this out. I have updated this post with a link.
Jeffrey D. King • 8:42 am on Dec. 22, 2009
@Andrew Hutchings #11
Thank you for pointing this out. It will be addressed in version 1.20.
Ghusse • 3:01 am on Jan. 4, 2010
Hi,
Is it possible to add two replacement possibilities : the strait quote to the curly one in any case; and the punctuation replacement.
The punctuation replacement has its interest with french language, where double punctuation signs (as “;”, “:”, “?” or “!”) must be preceded by an insecable space.
Thanks for your essential wp plugin and this php library.
Jenny • 7:59 am on Jan. 20, 2010
Hi Jeff,
Found a bug in version 1.20. php-typography.php, line 412, $customReplacementChunks is not initialized. From your indentation it looks like most of the function is supposed to be wrapped in the if statement, but you left out the curly brackets.
Jeffrey D. King • 9:28 am on Jan. 20, 2010
@Jenny #16
Yeah, that is some of the ugliest code I have written. I fixed it in version 1.21 about a week ago, but haven’t got to posting it yet. I will try to get it up today.
Jeffrey D. King • 12:23 pm on Jan. 22, 2010
@Jenny #16
Finally got around to posting 1.21. your issue should be resolved.