CodeIgniter 用户指南 版本 2.2.6

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

字符串辅助函数

该字符串辅助函数为你提供对字符串类型的各种函数。

装载字符串辅助函数

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

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

可用函数如下:

random_string()

根据你所指定的类型和长度产生一个随机字符串。可用于生成密码串或随机字串。

第一个参数指定字符串类型,第二个参数指定其长度。以下为可选字符串类型:

alpha, alunum, numeric, nozero, unique, md5, encrypt 和 sha1

范例:

echo random_string('alnum', 16);

increment_string()

通过附加一个可以增加的数生成一个字符串。Increments a string by appending a number to it or increasing the number. Useful for creating "copies" or a file or duplicating database content which has unique titles or slugs.

范例:

echo increment_string('file', '_'); // "file_1"
echo increment_string('file', '-', 2); // "file-2"
echo increment_string('file-4'); // "file-5"

alternator()

当执行一个循环时,让两个或两个以上的条目轮换使用。范例:

for ($i = 0; $i < 10; $i++)
{
    echo alternator('string one', 'string two');
}

你可以任意添加条目的数量,每一次循环后下一个条目将成为返回值。

for ($i = 0; $i < 10; $i++)
{
    echo alternator('one', 'two', 'three', 'four', 'five');
}

注意:为了让多次调用该函数简单方便,调用该函数时请不要带上实参进行重预置。

repeater()

重复生成你所提交的数据。范例:

$string = "\n";
echo repeater($string, 30);

上面的例子将会产生30个空行。

reduce_double_slashes()

将字符串中的双斜线(//)转换为单斜线(/),但不转换形如(http://)的双斜线。范例:

$string = "http://example.com//index.php";
echo reduce_double_slashes($string); // results in "http://example.com/index.php"

trim_slashes()

去掉任何出现在字符串开头或结尾的斜线。范例:

$string = "/this/that/theother/";
echo trim_slashes($string); // results in this/that/theother

reduce_multiples()

去掉多余的一个紧接着一个重复出现的特殊字符。范例:

$string="Fred, Bill,, Joe, Jimmy";
$string=reduce_multiples($string,","); //results in "Fred, Bill, Joe, Jimmy"

该函数可以接受如下的形参: reduce_multiples(string: text to search in, string: character to reduce, boolean: whether to remove the character from the front and end of the string) 第一个形参用于传送你所要去掉重复的字符串。第二个形参用于传送你所要去掉的字符。第三个形参默认为 False。如果为True将会去掉出现在字符串开头或结尾的字符(即使字符不重复也去掉)。范例: $string=",Fred, Bill,, Joe, Jimmy,";
$string=reduce_multiples($string, ", ", TRUE); //results in "Fred, Bill, Joe, Jimmy"

quotes_to_entities()

将字符串中的单引号和双引号转换为相应的 HTML 字符表示。范例:

$string="Joe's \"dinner\"";
$string=quotes_to_entities($string); //results in "Joe&#39;s &quot;dinner&quot;"

strip_quotes()

去掉字符串中的单引号和双引号。范例:

$string="Joe's \"dinner\"";
$string=strip_quotes($string); //results in "Joes dinner"

 

翻译贡献者: Hex, kkorange, popcorner
最后修改: 2014-06-05 11:29:39