Retired Documentation: You are using the documentation for version 1.7.3 which was retired in 2013. Go here for the latest version documentation or check here for your available upgrades to the latest version.
Typography Class
Calling the Typography Class
The Typography class is used in ExpressionEngine to parse type, providing tools for automatic XHTML, auto line-breaking, email encoding, word censoring, decoding pMcode, syntax highlighting, and gateway access to formatting plugins.
To use the these features in your modules, you need to first instantiate the Typography Class
if ( ! class_exists('Typography')) { require PATH_CORE.'core.typography'.EXT; } $TYPE = new Typography;
The class's constructor takes two arguments, $parse_images and $allow_headings, both of which default to TRUE unless you set them explicitly when instantiating the class. For example if you wanted to have {filedir_#} variables parsed, but did not want to allow <h#> tags in the text, you would instantiate the class thusly:
$TYPE = new Typography(TRUE, FALSE);
Typography Class Properties
The Typography class has a number of class properties that you may wish to set before calling any methods. Below is a description of available class properties and their default values (in bold).
- $allow_img_url
- (string) [ y / n ] — Allow inline images?
- $allow_js_img_anchors
- (bool) [ TRUE / FALSE ] — Whether to allow JavaScript submitted within <a href> and <img> tags
- $auto_links
- (string) [ y / n ] — Auto-link URLs and email addresses? (Note that auto-linking does not ever occur if $html_format is "none")
- $bounce
- (string) — Used to construct redirect links, to prevent control panel URLs from showing up in referrer logs, and on the front-end for rank denial. This property is set dynamically based on site preferences and is not necessary to set directly.
- $censored_replace
- (string) — String that censored words are replaced with, taken from site preferences.
- $censored_words
- (array) — Array of words to be censored, taken from site preferences.
- $code_chunks
- (array) — Array of temporary markers and content used to prevent formatting from being applied to syntax highlighted code.
- $code_counter
- (int) — Used as keys of the $code_chunks array, to keep the temporary markers organized.
- $convert_curly
- (bool) [ TRUE / FALSE ] — Convert curly brackets ( "{" and "}" ) into entities?
- $emoticon_path
- (string) — The preference setting for the URL path to the site's emoticons. This property is set dynamically based on site preferences and is not necessary to set directly.
- $encode_email
- (bool) [ TRUE / FALSE ] — Whether or not email addresses are encoded.
- $encode_type
- (string) [ javascript / noscript ] — Type of encoding applied to email addresses if email address encoding is enabled. "noscript" renders in a human readable format (e.g. "name at example dot com)", suitable for use where JavaScript is inappropriate, such as in a feed.
- $file_paths
- (array) — Array of file upload directories in key (ids) => value (urls) pairs.
- $highlight_code
- (bool) [ TRUE / FALSE ] — Perform PHP syntax highlighting on [pre] and [code] blocks?
- $html_fmt_types
- (array) [ array('safe', 'all', 'none') ] — Array of standard HTML handling types available to the Typography class.
- $html_format
- (string) [ safe / all / none ] — Controls how HTML is handled in text.
- $parse_images
- (bool) [ TRUE / FALSE ] — Whether or not {filedir_#} variables are to be parsed. Note that while the class defines this property as FALSE, the constructor's first argument sets this property directly. To set this property to FALSE, it is necessary to do so when instantiating the class, e.g.:
$TYPE = new Typography(FALSE);
- $parse_smileys
- (bool) [ TRUE / FALSE ] — Replace text smileys with smiley images?
- $popup_links
- (bool) [ TRUE / FALSE ] — Create links as popups?
- $single_line_pgfs
- (bool) [ TRUE / FALSE ] — Whether to treat single lines as paragraphs in auto-XHTML
- $site_index
- (string) — Deprecated.
- $smiley_array
- (mixed) [ FALSE ] — If emoticons are enabled for the site, this property will contain an array of smiley conversions in key (smiley) => value (image) pairs. If emoticons are not enabled, this will be set to FALSE.
- $text_fmt_plugins
- (array) — Array of available installed plugins.
- $text_fmt_types
- (array) [ array('xhtml', 'br', 'none', 'lite') ] — Array of standard formatting types available to the Typography class.
- $text_format
- (string) [ xhtml / br / none / lite ] — Controls what formatting is applied to text.
- $use_span_tags
- (bool) [ TRUE / FALSE ] — Use <span> tags for font color and size pMcode? Setting to FALSE uses <font> tags.
- $word_censor
- (bool) [ FALSE ] — Whether or not word censoring is applied. This property is set dynamically based on site preferences and is not necessary to set directly.
- $yes_no_syntax
- (array) [ array('y', 'n') ] — Array of valid Yes / No strings for use in properties. Used to ensure that valid settings are being provided for a Yes / No type preference.
Parsing Type
Description
str $TYPE->parse_type ( str $str, array $prefs )
This function returns a string of parsed type. It is the most common use of the Typography class, and many of the individual functions also described in this document are used within the parse_type() method. The format the string is returned in is determined by both the class properties and the array of properties provided in the second argument.
Using $TYPE->parse_type()
$str = $TYPE->parse_type($str);
Example of Parsing Type with Preferences
You may override class properties directly in the $prefs array for the following:
- text_format
- html_format
- auto_links
- allow_img_url
$prefs = array( 'text_format' => 'xhtml', 'html_format' => 'all', 'auto_links' => 'y', 'allow_img_url' => 'y' ); $str = $TYPE->parse_type($str, $prefs);
Using a Plugin for Text Formatting
Any installed formatting plugin may be used to parse type. Simply use the class name of the plugin, in lowercase letters.
$str = $TYPE->parse_type($str, array('text_format' => 'markdown'));
If you attempt to use a plugin that is not installed, no text formatting will be performed. It may be wise to check for the existence of plugins before using them, so if they are not installed, you can fall back on one of the native formatting types.
$text_format = (in_array('markdown', $TYPE->text_fmt_plugins)) ? 'markdown' : 'xhtml'; $str = $TYPE->parse_type($str, array('text_format' => $text_format));
Encode Email Addresses
Description
str $TYPE->encode_email ( str $email, str $title, bool $anchor )
This function encodes email addresses with Javascript, to assist in prevention of email harvesting by bots
- (string) — Email address. Required
- $title
- (string) [ empty string ] — The text to use as the title of the email link.
- $anchor
- (bool) [ TRUE / FALSE ] — Whether or not a clickable link is created for the email address.
Using $TYPE->encode_email()
$str = "[email protected]"; $str = $TYPE->encode_email($str, "Email Brett Bretterson");
If you want to return a human readable "encoded" email address instead, you can also set the $encode_type class property to "noscript".
$str = "[email protected]"; $TYPE->encode_type = "noscript"; $str = $TYPE->encode_email($str, '', FALSE);
Returns:
brett dot bretterson at example dot com
XTHML Typography
Description
str $TYPE->xhtml_typography ( str $str )
This function takes a string of text and returns typographically correct XHTML. It's primary modifications are:
- It turns double spaces into paragraphs.
- It adds line breaks where there are single spaces.
- It turns single and double quotes into curly quotes.
- It turns three dots into ellipsis.
- It turns double dashes into em-dashes.
- $str
- (string) Text to apply XHTML typography to
Using $TYPE->encode_email()
$str = $TYPE->xhtml_typography($str);
"Light" XTHML Typography
Description
str $TYPE->light_xhtml_typography ( str $str )
This function performs the character transformation portion of the XHTML typography only, i.e. curly quotes, ellipsis, etc.
- $str
- (string) Text to apply Light XHTML typography to
Using $TYPE->light_xhtml_typography()
$str = $TYPE->light_xhtml_typography($str);