状态 草稿
Todo Proof read
官方最后更新时间 2008/12/17 10:27

文本辅助函数

提供对文本处理的方法。

方法

limit_words()

text::limit_words() 接受多个参数。但输入类型必须为字符串型。默认的结束字符是省略号。

$long_description = 'The rain in Spain falls mainly in the plain';
$limit = 4;
$end_char = ' ';
 
$short_description = text::limit_words($long_description, $limit, $end_char);

返回结果:

The rain in Spain

limit_chars()

text::limit_chars() 接受多个参数。但输入类型必须为字符串型。默认的结束字符是省略号。

$long_description = 'The rain in Spain falls mainly in the plain';
$limit = 4;
$end_char = ' ';
$preserve_words = FALSE;
 
$short_description = text::limit_chars($long_description, $limit, $end_char, $preserve_words);

返回结果:

The r

alternate()

text::alternate() 接受多个参数。参数的数目随机输出。对于循环输出一些东西想要随机添加颜色很有帮助:

for($i=0:$i<5:$i++)
{
    echo text::alternate('1','2','boom');
}
//returns 12boom12

random()

text::random() 接受多个可选参数。返回指定长度的随机文本字符串。

可用的 $type 值的类型:

  • 字母数字 - 0-9, a-z 和 A-Z
  • 字母 - a-z, A-Z
  • 数字 - 0-9
  • 非零数字 - 1-9
  • distinct - Only distinct characters that can't be mistaken for others.
  • 不符合上述任何一项字符的值通过将被使用
echo text::random($type = 'alnum', $length = 10);

reduce_slashes()

text::reduce_slashes() reduces multiple slashes in a string to single slashes.

<?php
$str = "path/to//something";
echo reduce_slashes($str); // Outputs: path/to/something
?>

censor()

text::censor() accepts multiple optional parameters. The input string and an array of marker words is required. Returns a string with the marker words censored by the specified replacement character.

$str = 'The income tax is a three letter word, but telemarketers are scum.';
$replacement = '*';
$badwords = array('tax', 'scum');
 
echo text::censor($str, $badwords, $replacement, $replace_partial_words = FALSE);

Generates:

The income *** is a three letter word, but telemarketers are ****.

similar()

Finds the text that is similar between a set of words.

auto_link()

Converts text email addresses and anchors into links.

auto_link_urls()

Converts text anchors into links.

auto_link_emails()

Converts text email addresses into links.

bytes()

text::bytes($bytes,$force_unit,$format,$si) Returns a human readable size.

  • $bytes - Supply the number of bites
  • $force_unit - defaults to NULL when supplied the function will return in those units.
  • $format - format of the return string
  • $si - Defaults to TRUE, when FALSE function will return IEC prefixes (KiB, MiB etc.) else will return SI prefixes (kB, MB, GB etc)
echo text::bytes('2048'); //returns 2.05 kB
echo text::bytes('4194304','kB'); //returns 4194.30 kB
echo text::bytes('4194304','GiB'); //returns 0.00 GiB
echo text::bytes('4194304',NULL, NULL, FALSE); //returns 4.00 MiB

widont()

text::widont() Returns a string without widow words by inserting a non-breaking space between the last two words. A widow word is a single word at the end of a paragraph on a new line. It's considered bad style.

  • $string - String with potential widow words
$paragraph='Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras id dolor. Donec ...';
 
$paragraph=text::widont($paragraph);

auto_p()

Automatically applies <p> and <br /> markup to text. Basically nl2br() on steroids.

helpers/text.txt · 最后更改: 2009/01/05 23:16 由 icyleaf