文本輔助函數(shù)
文本輔助函數(shù)所包含的函數(shù)只能對(duì)文本進(jìn)行處理。
裝載文本輔助函數(shù)
采用如下方式裝載該輔助函數(shù):
$this->load->helper('text');
可用函數(shù)如下:
word_limiter()
根據(jù)指定的詞語(yǔ)(由于是英語(yǔ),對(duì)中文應(yīng)該是以空格為判斷標(biāo)準(zhǔn),譯者注)數(shù)目對(duì)一段字符串進(jìn)行截取。范例:
$string = "Here is a nice text string consisting of eleven words.";
$string = word_limiter($string, 4);
// Returns: Here is a nice…
第三個(gè)參數(shù)是一個(gè)可選的符號(hào)后綴,默認(rèn)在截取段后加上省略號(hào)(…)。
character_limiter()
根據(jù)指定的字符數(shù)目對(duì)一段字符串進(jìn)行截取。它將會(huì)保證單詞的完整性(對(duì)英語(yǔ)單詞而言,譯者注),因此可能會(huì)造成截取后的字符數(shù)目與指定的有一點(diǎn)出入。范例:
$string = "Here is a nice text string consisting of eleven words.";
$string = character_limiter($string, 20);
// Returns: Here is a nice text string…
第三個(gè)參數(shù)是一個(gè)可選的符號(hào)后綴,默認(rèn)在截取段后加上省略號(hào)(…)。
ascii_to_entities()
將ASCII碼轉(zhuǎn)換為字符實(shí)體,包括那些在網(wǎng)頁(yè)中使用時(shí)可能導(dǎo)致問(wèn)題的高位ASCII碼和微軟Word字符,因此它們能夠被正確地顯示出來(lái),不受瀏覽器設(shè)置或者數(shù)據(jù)庫(kù)可靠存儲(chǔ)的影響。本函數(shù)部分依賴于你的服務(wù)器對(duì)字符集的支持,因此并不是在任何情況下都100%的可靠,但在大多數(shù)情況下都可以正確地識(shí)別正常范圍以外的字符(比如重音符號(hào))。例如:
$string = ascii_to_entities($string);
entities_to_ascii()
這個(gè)函數(shù)與ascii_to_entities()功能相反; 它將字符轉(zhuǎn)變?yōu)锳SC碼.
convert_accented_characters()
Transliterates high ASCII characters to low ASCII equivalents, useful when non-English characters need to be used where only standard ASCII characters are safely used, for instance, in URLs.
$string = convert_accented_characters($string);
This function uses a companion config file application/config/foreign_chars.php to define the to and from array for transliteration.
word_censor()
讓你可以對(duì)文本中的文字進(jìn)行審核替換。第一個(gè)形參用于獲取原始字符串。第二個(gè)形參用于存放你不允許的文字的數(shù)組。第三個(gè)形參(可選)用于存放一個(gè)替換不允許文字的字段。如果不指定則被替換為“磅”的表示符號(hào):####。范例:
$disallowed = array('darn', 'shucks', 'golly', 'phooey');
$string = word_censor($string, $disallowed, 'Beep!');
highlight_code()
對(duì)一段代碼(PHP,HTML等)進(jìn)行著色。例如:
$string = highlight_code($string);
本函數(shù)使用PHP的 highlight_string() 函數(shù),因此所使用的顏色是你在 php.ini 文件中指定的那些。
highlight_phrase()
對(duì)字符串內(nèi)的一個(gè)短語(yǔ)進(jìn)行突出顯示。第一個(gè)參數(shù)是原始字符串,第二個(gè)參數(shù)是你想要突出顯示的短語(yǔ)。如果要用HTML標(biāo)簽對(duì)短語(yǔ)進(jìn)行標(biāo)記,那么第三個(gè)和第四個(gè)參數(shù)分別是你想要對(duì)短語(yǔ)使用的HTML打開(kāi)和關(guān)閉標(biāo)簽。例如:
$string = "Here is a nice text string about nothing in particular.";
$string = highlight_phrase($string, "nice text", '<span style="color:#990000">', '</span>');
以上內(nèi)容將返回:
Here is a nice text string about nothing in particular.
word_wrap()
根據(jù)指定的字符數(shù)目對(duì)文本進(jìn)行換行操作,并且保持詞語(yǔ)的完整性(對(duì)英語(yǔ)單詞而言,筆者注)。范例:
$string = "Here is a simple string of text that will help us demonstrate this function.";
echo word_wrap($string, 25);
// Would produce:
Here is a simple string
of text that will help
us demonstrate this
function
ellipsize()
過(guò)濾字符串中的標(biāo)簽,在指定的最大長(zhǎng)度處切割字符串,并插入一個(gè)省略號(hào)。
第一個(gè)參數(shù)是要被省略處理的字符串,第二個(gè)參數(shù)是目標(biāo)字符串中字符的數(shù)目。第三個(gè)參數(shù)是省略號(hào)在目標(biāo)字符串的位置,值為0 - 1,從左到右。例如,值為1時(shí),省略號(hào)位于目標(biāo)字符串的右端,值為.5時(shí),位于中間,而值為0時(shí)位于左端。
第四個(gè)參數(shù)是可選的,用于指明使用的省略符號(hào)類型,默認(rèn)使用 … 。
$str = 'this_string_is_entirely_too_long_and_might_break_my_design.jpg';
echo ellipsize($str, 32, .5);
輸出:
this_string_is_e…ak_my_design.jpg
?