CodeIgniter 用户指南 版本 1.6.3

编辑文档、查看近期更改请 登录注册  找回密码
查看原文

文本辅助函数

文本辅助函数所包含的函数只能对文本进行处理。

装载文本辅助函数

采用如下方式装载该辅助函数:

$this->load->helper('text');

可用函数如下:

word_limiter()

根据指定的词语(由于是英语,对中文应该是以空格为判断标准,笔者注)数目对一段字符串进行截取。范例:

$string = "Here is a nice text string consisting of eleven words.";

$string = word_limiter($string, 4);

// Returns: Here is a nice…

第三个形参是一个可选的符号后缀,默认在截取段后加上省略号(…)。

character_limiter()

根据指定的字符数目对一段字符串进行截取。它将会保证单词的完整性(对英语单词而言,笔者注),因此可能会造成截取后的字符数目与指定的有一点出入。范例:

$string = "Here is a nice text string consisting of eleven words.";

$string = character_limiter($string, 20);

// Returns: Here is a nice text string…

第三个形参是一个可选的符号后缀,默认在截取段后加上省略号(…)。

ascii_to_entities()

Converts ASCII values to character entities, including high ASCII and MS Word characters that can cause problems when used in a web page, so that they can be shown consistently regardless of browser settings or stored reliably in a database. There is some dependence on your server's supported character sets, so it may not be 100% reliable in all cases, but for the most part it should correctly identify characters outside the normal range (like accented characters). Example:

$string = ascii_to_entities($string);

entities_to_ascii()

这个函数与ascii_to_entities()功能相反; 它将字符转变为ASC码.

word_censor()

让你可以对文本中的文字进行审核替换。第一个形参用于获取原始字符串。第二个形参用于存放你不允许的文字的数组。第三个形参(可选)用于存放一个替换不允许文字的字段。如果不指定则被替换为“磅”的表示符号:####。范例:

$disallowed = array('darn', 'shucks', 'golly', 'phooey');

$string = word_censor($string, $disallowed, 'Beep!');

highlight_code()

Colorizes a string of code (PHP, HTML, etc.). Example:

$string = highlight_code($string);

The function uses PHP's highlight_string() function, so the colors used are the ones specified in your php.ini file.

highlight_phrase()

Will highlight a phrase within a text string. The first parameter will contain the original string, the second will contain the phrase you wish to highlight. The third and fourth parameters will contain the opening/closing HTML tags you would like the phrase wrapped in. Example:

$string = "Here is a nice text string about nothing in particular.";

$string = highlight_phrase($string, "nice text", '<span style="color:#990000">', '</span>');

The above text returns:

Here is a nice text string about nothing in particular.

word_wrap()

根据指定的字符数目对文本进行换行操作,并且保持词语的完整性(对英语单词而言,笔者注)。范例:

$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

 

翻译贡献者: Hex, kkorange, kntism
最后修改: 2008-10-26 13:44:00