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