PHP Typography 1.21

Features

PHP Typog­ra­phy is a PHP based solu­tion to greatly improve web typog­ra­phy. It fea­tures the fol­low­ing capa­bil­i­ties (includ­ing gran­u­lar control):

  • Hyphen­ation
  • Spac­ing con­trol, includ­ing: glu­ing val­ues to units, widow pro­tec­tion, and forced inter­nal wrap­ping of long URLs & email addresses.
  • Intel­li­gent char­ac­ter replace­ment, includ­ing smart han­dling of: quote marks ( “foo” ), dashes ( foo – bar ), ellipses ( … ), trade­marks ( ™ ), math sym­bols ( 1024×768 ), frac­tions ( 1223 ), and ordi­nal suf­fixes ( 3rd )
  • CSS hooks for styling: amper­sands (class “amp”), acronyms (class “caps”), num­bers (class “num­bers”), ini­tial sin­gle quotes (class “quo”), and ini­tial dou­ble quotes & guillemets (class “dquo”).

This project merges, builds upon, and gen­er­al­izes the func­tion­al­ity of wp-​​Typogrify, wp-​​Hyphenate and SmartyPants.

Implementation

While com­pli­ca­tion is added when manip­u­lat­ing the many options, imple­ment­ing PHP Typog­ra­phy 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 Typog­ra­phy is designed for easy inte­gra­tion with plu­gin archi­tec­tures of PHP based con­tent man­age­ment sys­tems. It has already been ported to Word­Press and Zikula CMS. We would like to see other devel­op­ers pick up this code and imple­ment it for other CMSs. Specifically:

  • Dru­pal
  • Joomla
  • Expres­sion Engine

If you are a devel­oper for these (or any other PHP based CMS), the heavy lift­ing is done. You just need to hook it into your CMS.

Testimonial

Here is a fine tes­ti­mo­nial from Jenny (who also pro­vided some valu­able feed­back to fur­ther improve the project):

I’ve been eval­u­at­ing PHP Typog­ra­phy for use in a cus­tom CMS and have been very pleased so far…. It was, as you promised, easy to inte­grate in our frame­work (ZF) by wrap­ping it in a sim­ple class imple­ment­ing Zend_​Filter_​Interface. I am using PHP Typog­ra­phy after run­ning the text through HTMLPu­ri­fier, and the out­put has been beau­ti­ful thus far.

Requirements

PHP Typog­ra­phy has the fol­low­ing requirements:

  • the host server must run PHP 5 or later
  • text must be encoded UTF-​​8
  • all markup must be valid xHTML, specifically:
    • every ele­ment must be closed,
    • every attribute must have a value enclosed in quotes, and
    • tag names and attrib­utes must be lowercase.

Related

It should also be noted, PHP Typog­ra­phy would not have been pos­si­ble with­out another tool we have recently released: PHP Parser. PHP Parser is an amaz­ingly pow­er­ful resource that allows HTML to be parsed, mod­i­fied and recon­structed in ways pre­vi­ously not possible.

Your feed­back is much appre­ci­ated. How can we make this plu­gin bet­ter? Have you devel­oped a plu­gin using PHP Typog­ra­phy? Email us at info@​kingdesk.​com

Comments

  1. A com­menter on the wp-​​Typography project noti­fied us a false pos­i­tive 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 con­firmed false pos­i­tives for the cy.php, en-GB.php, es.php, ga.php, hr.php, and pr.php lan­guage files. We have been unable to divine the cause. We have reported the false pos­i­tives to Avira and pro­vided them with a copy of the files.

    Avira’s review of the faslse pos­i­tives may be viewed here.

    We have also tested the files in AVG, and are happy to report no false pos­i­tives with their anti-​​virus solution.

  2. From your arti­cle I def­i­nitely think that PHP typog­ra­phy sounds great. I will down­load it, try it and will con­tact you after that.


    Web­de­sign Stuttgart

  3. Does the hyphen­ation pro­vide an excep­tion for cap­i­tal­ized words? I love the Hyphen­ate plu­gin, but pro­tect­ing proper nouns is essen­tial for many pub­li­ca­tions, so I can’t use it if I can’t fil­ter cap­i­tal­ized words.

  4. @Stephen Coles #24

    A new method set_hyphenate_title_case() was added to ver­sion 1.5 to meet your need.

  5. The new WP-​​MU beta plu­gin is great news. I tried it out, and this time it actu­ally works under MU as well. Or at least, I can change the hyphen­ation lan­guage this time…
    :-)

    BTW: A ter­rific fea­ture would be if the default hyphen­ation lan­guage would be the same as the blog lan­guage. Even more so on a MU site, when I acti­vate that plu­gin for all blogs. And how would you deal with a mul­ti­lan­guage blog? Most of the blogs on my MU install are ger­man, some of them eng­lish, and a few are multi-​​language (ger­man, eng­lish, french, maybe ital­ian, take your choice — the last three are offi­cial lan­guages within Switzer­land), where the user can switch to his favorite lan­guage on-​​the-​​fly.

    Thanks a lot for a won­der­ful plu­gin!
     – Daniel

  6. Hi,

    the typog­ra­phy plu­gin is really great. It just hap­pens to make a mis­take with dates writ­ten in ISO for­mat, e.g. “2009 – 08-​​02″. In these, the first hyphen is enlarged, the sec­ond is not. The whole date looks quite gar­bled. Per­haps you can fix that in some new release? Apart from this, the pro­duced out­put is really nice and improves the “usage expe­ri­ence” of “the aver­age blog” quite con­sid­er­ably. Thank you!

    Best regards,
    Dirk

  7. @Dirk Hill­brecht #5

    Thank you for bring­ing this issue to my atten­tion. It has been cor­rected in ver­sion 1.8

  8. …just installed 1.8 and date for­mat­ting now works mar­vel­lous. Thanks again for this great piece of soft­ware! Dirk

  9. There seems to be bug in style_​initial_​quotes(). All 3 mb_​substr() there are with­out encod­ing para­me­ter. Only after adding “UTF-​​8″ it works.

  10. While inte­grat­ing this with my project (run­ning on Kohana VO3) I ran into some prob­lems. It works great until it finds any tags, classes, or IDs set to ignore. I’m not sure what the prob­lem is, but when I try to add any­thing that is listed in the settings[“ignoreTags”], settings[“ignoreClasses”], or settings[“ignoreIDs”] arrays of the php­Ty­pog­ra­phy class, I get an error that says the following:

    ErrorEx­cep­tion [ Notice ]: Unde­fined index: openPos.

  11. @Priit Pirita #8 & Bill # 9,

    Both your issues should be resolved in the new ver­sion 1.19 release.

  12. I found a bug in ver­sion 1.19: on line 1950, $encod­ings is used with­out being ini­tial­ized. Oth­er­wise, works great!

  13. @Erik Spann #12

    Thanks for point­ing this out. I have updated this post with a link.

  14. @Andrew Hutch­ings #11

    Thank you for point­ing this out. It will be addressed in ver­sion 1.20.

  15. Hi,

    Is it pos­si­ble to add two replace­ment pos­si­bil­i­ties : the strait quote to the curly one in any case; and the punc­tu­a­tion replacement.

    The punc­tu­a­tion replace­ment has its inter­est with french lan­guage, where dou­ble punc­tu­a­tion signs (as “;”, “:”, “?” or “!”) must be pre­ceded by an inse­ca­ble space.

    Thanks for your essen­tial wp plu­gin and this php library.

  16. Hi Jeff,

    Found a bug in ver­sion 1.20. php-typography.php, line 412, $cus­tom­Re­place­mentChunks is not ini­tial­ized. From your inden­ta­tion it looks like most of the func­tion is sup­posed to be wrapped in the if state­ment, but you left out the curly brackets.

  17. @Jenny #16

    Yeah, that is some of the ugli­est code I have writ­ten. I fixed it in ver­sion 1.21 about a week ago, but haven’t got to post­ing it yet. I will try to get it up today.

  18. @Jenny #16

    Finally got around to post­ing 1.21. your issue should be resolved.

  19. This looks inter­est­ing. I’d writ­ten my own ‘typog­ra­phy engine’, but this it prob­a­bly bet­ter made than mine. Going to com­pare fea­tures. Keep up the good work.

  20. great tool!!!

  21. Wow. Just knew PHP Typog­ra­phy as WordPress-​​Plugin, inte­grated it in http://​www​.kikidu​.de and it works – fan­tas­tic! Thanks for your great efforts!

  22. +1 Ghusse

    Does it replace the space in these par­tic­u­lar cases* with alt+0160?

    http://fr.wikipedia.org/wiki/Espace_ins%C3%A9cable
    *”Le code typographique français recom­mande, con­traire­ment à d’autres langues telles que l’anglais, une espace insé­ca­ble devant les signes de ponc­tu­a­tion dou­bles (point-​​virgule, deux-​​points, point d’interrogation, point d’exclamation), entre les guillemets et le texte qu’ils ren­fer­ment, ainsi que comme sépara­teur des groupes de car­ac­tères (sépara­teur de groupes de 3 chiffres dans les nom­bres supérieurs au mil­lier, sépara­teur sans valeur facil­i­tant la lec­ture des numéros de télé­phone ou des numéros et codes d’identification, etc.).”

    No-​​break thin space, known in Uni­code as “NARROW NO-​​BREAK SPACE” (U+202F). This is required for French punc­tu­a­tion (before ?, !, : or ;).

  23. and “espace fine insé­ca­ble : &#x202F”

  24. Love the poten­tial of your tools, and would love them more if you could make them a bit more acces­si­ble for those of us who haven’t made it to the 500 level. But whether or not I man­age to imple­ment your mod­ules, I truly appre­ci­ate your sim­ply offer­ing the code (because proper quotes matter).

    The fol­low­ing are notions from my trainer/​tech writer side, in case any resonate:

    - A clear “What it affects and what it shouldn’t man­gle” for those who can’t read PHP like Eng­lish, don’t nec­es­sar­ily have time to exper­i­ment, and get strangely twitchy about unknown per­va­sive code ;-)

    - Examples/​recipes/​snippets – even with­out com­ments some­thing real is so use­ful vs. try­ing to grok a mod­ule as theory.

    - HTML or PDF addi­tion to the cur­rent docs for absorb­ing big pic­tures from gen­eral con­cepts (hyphen­ation, frac­tions), rather than small bites from known terms (set_​dewidow).

    - Could you add to http://​king​desk​.com/​p​r​o​j​e​c​t​s​/​p​h​p​-​t​y​p​o​g​r​a​p​h​y​-​d​o​c​u​m​e​n​t​a​t​i​on/ a set of String/​Action/​etc. links? Or maybe turn it into a table indi­cat­ing what’s what? Took me a cou­ple min­utes to real­ize there were 2+ des­ti­na­tion pages :-)

    - In the func­tion ref­er­ence, a cou­ple instances of the code boxes such as for “set_​​tags_​​to_​​ignore()” seem to crop on print. Right now I think that info is rel­a­tively minor, but just wanted to men­tion for future writ­ing or style updates. (Keep see­ing some great wrapped #ed line for­mats on blogs – must be a plu­gin or JS framework?)

  25. I was try­ing to use this with -moz-column-count & -webkit-column-count and it doesn’t seem to make any dif­fer­ence. Is there a way to make it work with columns? Thanks!

  26. Thanks for an excel­lent piece of soft­ware! I have a few com­ments based on my expe­ri­ence of it in Word­Press which I’d like to share.

    With ref­er­ence to what I’ve read in “The Oxford Guide to Style” and “The Ele­ments of Typo­graphic Style”, I’ve always under­stood that there is a choice between using spaced en dashes (e.g., “nearly – but not quite!”) or close-​​set em dashes (e.g., “nearly — but not quite!”) between words. Most US pub­lish­ers adopt the lat­ter, most UK pub­lish­ers the for­mer. I would like to see a choice between these styles in PHP Typog­ra­phy, which cur­rently uses a rather uncon­ven­tial style – spaced em dashes.

    Sim­i­larly, with numer­i­cal ranges (e.g., “1950 – 1953″), the accepted form is close-​​set en dashes, but in this instance PHP Typog­ra­phy uses spaced en dashes. Could this also have its own option?

    Thanks again.

  27. I’ve just realised the soft­ware has for­mat­ted the exam­ples in my pre­vi­ous com­ment so the points I’m mak­ing aren’t ter­ri­bly clear, but I hope it still makes sense nevertheless.

  28. Hello,

    Has any­one tried using this plug-​​in to hyphen­ate text returned by Flut­ter? In case any­one has please post how you got it working.

    Thank you!

  29. Hi,

    is it pos­si­ble to con­fig­ure php­Ty­pog­ra­phy to return HTML enti­ties instead of encoded UTF-​​8 characters?

  30. Thank you very much. You saved me a lot of work. There are some bugs in pol­ish lan­guage, but still script works very good.

  31. Hey there!

    This library is pretty cool, but there’s one thing I noticed: if you end your quote with a digit it is con­sid­ered as a inch symbol.

    eg. This is a “test 23″.

    Hope you can fix that!

  32. This is nice but it is very slow (in the mean­ing of uses a lot of cpu). Did you ever thought about add some caching?

Leave a Comment

Please note our comment and privacy policies.

(required)
(required)

Trackbacks

  1. […] here: PHP Typog­ra­phy 1.0 beta 4 • KING­desk Share and […]

  2. […] A script named php-​​typography (Also avail­able in a word­press plu­gin) enables more advanced type­set­ting options. Some […]

  3. […] wp-​​Typography ist­nieje także jako zwykły kod PHP, który można przys­tosować do włas­nych potrzeb (np. w innych niż Word­Press CMS-​​ach). Szczegółowe infor­ma­cje na ten temat dostępne są na stronie inter­ne­towej autora (PHP Typography). […]

  4. […] sagen wäre: die Auto­ren stel­len die Funk­tio­nen auch ohne Word­press zum Ein­bin­den in eigene PHP-​​Projekte zur Ver­fü­gung Diese Icons ver­lin­ken auf Book­mark Dien­ste bei denen Nut­zer neue Inhalte […]

  5. […] mas sim­ple con este plu­gin. wp-​​Typography es un proyecto de KING­desk y su código se basa en el PHP Typog­ra­phy. Al ser con­fig­u­rado como nos sea mas con­ve­niente, pro­cede a hacer reem­pla­zos automáti­cos de […]

  6. […] wp-​​Typography WP-​​Typography is one for all the type nerds out there. It adds hyphen­a­tion, spa­cing con­trol, intel­li­gent char­ac­ter replace­ment, and CSS hooks, all of which greatly improve the look and feel of your blog. At the moment I have the hyphen­a­tion, char­ac­ter replace­ment, and widow pre­ven­tion turned on. […]

  7. […] Ist halt lei­der schwierig wenn’s nicht nach den US-​​Regeln gehen soll. Pro­biert mal die von Kingdesk […]

  8. […] PHP Typog­ra­phy 1.12 que viene tam­bién como plu­gin para WordPress […]

  9. […] can eas­ily be ported to any other PHP based con­tent man­age­ment sys­tem. A sis­ter project — PHP Typog­ra­phy assem­bles all typo­graphic func­tion­al­ity (with­out any Word­Press spe­cific code) in an object oriented […]

  10. […] top right are now picked from all the images pos­ted so far. Finally, thanks to the won­der­ful php-​​​typography class the typo­graphy of the posts should be cleaner, and best of all: there’s now […]

  11. […] que “ty­po­gra­phie” né veut pas dire “choi­sir une po­lice”. Ai­dez vos clients à amé­lio­rer le rendu ty­po­gra­phique de leus conte­nus grâce à des lignes CSS, des plu­gins comme wp-​​typography ou des mo­dules PHP comme php-​​typography. […]

  12. […] given string or URL, gen­er­ate a descrip­tion and does all with not using any banned words you define.PHP Typog­ra­phyA PHP class for improv­ing web typog­ra­phy with fea­tures like hyphen­ation, spac­ing con­trol, wrapping […]

  13. […] PHP Typography […]

  14. […] PHP Typography […]

  15. […] PHP Typography […]

  16. […] PHP Typography […]

  17. […] PHP Typography […]

  18. […] wp-​​​Typography Wer­sja pol­ska — PO­BIERZ wp-typography-pl.zip [1,1 MB] Wty­czka w re­po­zy­to­rium Word­Pressa Strona do­mowa Li­cencja FAQ PHP Typography […]

  19. […] fig­ured out how to plug PHP Typog­ra­phy into Sym­fony so I could ren­der my sen­tences with smart quotes and nice hyphen­ation. And learned a […]

  20. Обработка текстов разными типограферами…

    Для сравнения и проверки качества взял короткий, но показательный текст примера и прогнал через 3 (известных мне, их может существовать и больше) типографера. Результаты каждый может оценить самостоятельно, или по книгам (…

  21. […] Php-​​typography jest łatwy i wygodny w użyciu. […]

  22. […] PHP Typography […]

  23. […] php typo­gra­phy, une abs­trac­tion), suc­ces­seur de Typo­grify, est une excel­lente exten­sion pour […]

  24. […] PHP Typography […]

  25. […] PHP Typography […]