Typography

The Typography library contains methods that help you format textin semantically relevant ways.

Loading the Library

Like all services in CodeIgniter, it can be loaded via Config\Services, though you usually will not needto load it manually:

  1. $typography = \Config\Services::typography();

Available static functions

The following functions are available:

autoTypography()

  • autoTypography($str[, $reduce_linebreaks = FALSE])

Parameters:

  • $str (string) – Input string
  • $reduce_linebreaks (bool) – Whether to reduce multiple instances of double newlines to twoReturns:HTML-formatted typography-safe stringReturn type:string

Formats text so that it is semantically and typographically correctHTML.

Usage example:

  1. $string = $typography->autoTypography($string);

Note

Typographic formatting can be processor intensive, particularly ifyou have a lot of content being formatted. If you choose to use thisfunction you may want to consider caching yourpages.

formatCharacters()

  • formatCharacters($str)

Parameters:

  • $str (string) – Input stringReturns:String with formatted characters.Return type:string

This function mainly converts double and single quotesto curly entities, but it also converts em-dashes,double spaces, and ampersands.

Usage example:

  1. $string = $typography->formatCharacters($string);

nl2brExceptPre()

  • nl2brExceptPre($str)

Parameters:

  • $str (string) – Input stringReturns:String with HTML-formatted line breaksReturn type:string

Converts newlines to tags unless they appear within

  1. tags.This function is identical to the native PHP nl2br() function,except that it ignores
     tags.

  2. Usage example:

  3. $string = $typography->nl2brExceptPre($string);