排版类
排版类提供帮助你格式化文本的函数。
初始化排版类
和CI中的其他类一样, 排版类也需要在控制器中这样用 $this->load->library 方法初始化:
$this->load->library('typography');
一旦被加载, 排版类的对象就可以这样使用: $this->typography
auto_typography()
格式化文本以便纠正语义和印刷的错误HTML代码。输入一个字符串输出如下的格式化文本:
- 用一对P标签包住段落(看起来像是用两条线把段落分隔开似的)。
- 除了出现 <pre> 标签外,所有的单个线的分割被转换为 <br />。
- 块级别的元素,如<div>标签,没有被段落包装,但是如果他们包含段落的话就会包含文本。
- 除了出现在标签中的引号外,引号会被转换成正确的实体。
- 撇号“'”被转换为相应的实体。
- 双破折号 (像 -- 或--) 被转换成 em—破折号.
- 三个连续的点也会被转换为省略号…
- 句子后连续的多个空格将被转换为 以便在网页中显示。
例如:
$string = $this->typography->auto_typography($string);
参数
有一个可选参数:布尔值 TRUE 或 FALSE决定是否对超过两个的换行进行压缩,减少到两行。
默认不压缩. 也就是说, 如果这个参数不设置, 它将如下工作:
$string = $this->typography->auto_typography($string, FALSE);
提示: 排版格式化可以处理密集数据, 特别是你有很多内容需要格式化处理。如果你选择这个函数处理,你可以考虑缓存你的网页。
format_characters()
This function is similar to the auto_typography function above, except that it only does character conversion:
- Quotes are converted to correctly facing curly quote entities, except those that appear within tags.
- Apostrophes are converted to curly apostrophe entities.
- Double dashes (either like -- this or like--this) are converted to em—dashes.
- Three consecutive periods either preceding or following a word are converted to ellipsis…
- Double spaces following sentences are converted to non-breaking spaces to mimic double spacing.
Usage example:
$string = $this->typography->format_characters($string);
nl2br_except_pre()
Converts newlines to <br /> tags unless they appear within <pre> tags. This function is identical to the native PHP nl2br() function, except that it ignores <pre> tags.
Usage example:
$string = $this->typography->nl2br_except_pre($string);
protect_braced_quotes
When using the Typography library in conjunction with the Template Parser library it can often be desirable to protect single and double quotes within curly braces. To enable this, set the protect_braced_quotes class property to TRUE.
Usage example:
$this->load->library('typography');
$this->typography->protect_braced_quotes = TRUE;