排版類
排版類提供幫助你格式化文本的函數(shù)。
初始化排版類
和CI中的其他類一樣, 排版類也需要在控制器中這樣用 $this->load->library 方法初始化:
$this->load->library('typography');
一旦被加載, 排版類的對象就可以這樣使用: $this->typography
auto_typography()
格式化文本以便糾正語義和印刷的錯誤HTML代碼。輸入一個字符串輸出如下的格式化文本:
- 用一對P標簽包住段落(看起來像是用兩個換行符把段落分隔開似的)。
- 除了出現(xiàn) <pre> 標簽外,所有的單個換行符被轉(zhuǎn)換為 <br />。
- 塊級別的元素,如<div>標簽,沒有被段落包裝,但是如果他們包含段落的話就會包含文本。
- 除了出現(xiàn)在標簽中的引號外,引號會被轉(zhuǎn)換成正確的實體。
- 撇號“'”被轉(zhuǎn)換為相應(yīng)的實體。
- 雙破折號 (像 -- 或--) 被轉(zhuǎn)換成 em—破折號.
- 三個連續(xù)的點也會被轉(zhuǎn)換為省略號…
- 句子后連續(xù)的多個空格將被轉(zhuǎn)換為 以便在網(wǎng)頁中顯示。
例如:
$string = $this->typography->auto_typography($string);
參數(shù)
有一個可選參數(shù):布爾值 TRUE 或 FALSE決定是否對超過兩個的換行進行壓縮,減少到兩行。
默認不壓縮. 也就是說, 如果這個參數(shù)不設(shè)置, 它將如下工作:
$string = $this->typography->auto_typography($string, FALSE);
提示: 排版格式化可以處理密集數(shù)據(jù), 特別是你有很多內(nèi)容需要格式化處理。如果你選擇這個函數(shù)處理,你可以考慮緩存你的網(wǎng)頁。
format_characters()
This function is similar to the auto_typography function above, except that it only does character conversion:
試翻譯:此函數(shù)與上面出現(xiàn)的auto_typography函數(shù)類似, 唯一不同的是此函數(shù)僅對以下字符進行轉(zhuǎn)換:
- Quotes are converted to correctly facing curly quote entities, except those that appear within tags.
- 試翻譯:除了在標簽中的引號外,其他引號將被轉(zhuǎn)換成字符實體.
- Apostrophes are converted to curly apostrophe entities.
- 試翻譯:省略號將被轉(zhuǎn)換成正確的格式.
- Double dashes (either like -- this or like--this) are converted to em—dashes.
- 試翻譯:雙破折號(比如 -- 或--) 將被轉(zhuǎn)換成 em—符號.
- Three consecutive periods either preceding or following a word are converted to ellipsis…
- 試翻譯:任何位置(詞前或詞后)的三個連續(xù)點號將被轉(zhuǎn)換成省略號…
- Double spaces following sentences are converted to non-breaking spaces to mimic double spacing.
- 試翻譯:句末的雙空格將被轉(zhuǎn)換成看似雙空格的一個整體符號.
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.
試翻譯: 除了 <pre> 標簽內(nèi)的新行,將被轉(zhuǎn)換為 <br /> 標簽. 除了本函(方法)數(shù)支持 <pre> 標簽外, 其與PHP內(nèi)置函數(shù) nl2br() 相同.
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;
?