字符串辅助函数

字符串辅助函数文件包含了一些帮助你处理字符串的函数。

重要

Please note that these functions are NOT intended, norsuitable to be used for any kind of security-related logic.

加载辅助函数

该辅助函数通过下面的代码加载:

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

可用函数

该辅助函数有下列可用函数:

  • randomstring([$type = 'alnum'[, $len = 8_]])

参数:

  • $type (string) — Randomization type
  • $len (int) — Output string length返回:A random string返回类型:string

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

第一个参数指定字符串类型,第二个参数指定其长度。有下列几种字符串类型可供选择:

  • alpha: 只含有大小写字母的字符串
  • alnum: 含有大小写字母以及数字的字符串
  • basic: 根据 mt_rand() 函数生成的一个随机数字
  • numeric: 数字字符串
  • nozero: 数字字符串(不含零)
  • md5: 根据 md5() 生成的一个加密的随机数字(长度固定为 32)
  • sha1: 根据 sha1() 生成的一个加密的随机数字(长度固定为 40)使用示例:
  1. echo random_string('alnum', 16);

注解

uniqueencrypt 类型已经废弃,它们只是 md5sha1 的别名。

  • incrementstring($str[, $separator = ''[, $first = 1]])

参数:

  • $str (string) — Input string
  • $separator (string) — Separator to append a duplicate number with
  • $first (int) — Starting number返回:An incremented string返回类型:string

自增字符串是指向字符串尾部添加一个数字,或者对这个数字进行自增。这在生成文件的拷贝时非常有用,或者向数据库中某列(例如 title 或 slug)添加重复的内容,但是这一列设置了唯一索引时。

使用示例:

  1. echo increment_string('file', '_'); // "file_1"
  2. echo increment_string('file', '-', 2); // "file-2"
  3. echo increment_string('file_4'); // "file_5"
  • alternator($args)

参数:

  • $args (mixed) — A variable number of arguments返回:Alternated string(s)返回类型:mixed

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

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

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

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

注解

如果要多次调用该函数,可以简单的通过不带参数重新初始化下。

  • repeater($data[, $num = 1])

参数:

  • $data (string) — Input
  • $num (int) — Number of times to repeat返回:Repeated string返回类型:string

重复生成你的数据。例如:

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

上面的代码会生成 30 个空行。

注解

该函数已经废弃,使用原生的 str_repeat() 函数替代。

  • reducedouble_slashes($str_)

参数:

  • $str (string) — Input string返回:A string with normalized slashes返回类型:string

将字符串中的双斜线('//')转换为单斜线('/'),但不转换 URL 协议中的双斜线(例如:http://

示例:

  1. $string = "http://example.com//index.php";
  2. echo reduce_double_slashes($string); // results in "http://example.com/index.php"
  • stripslashes($data_)

参数:

  • $data (mixed) — Input string or an array of strings返回:String(s) with stripped slashes返回类型:mixed

移除一个字符串数组中的所有斜线。

示例:

  1. $str = array(
  2. 'question' => 'Is your name O\'reilly?',
  3. 'answer' => 'No, my name is O\'connor.'
  4. );
  5.  
  6. $str = strip_slashes($str);

上面的代码将返回下面的数组:

  1. array(
  2. 'question' => "Is your name O'reilly?",
  3. 'answer' => "No, my name is O'connor."
  4. );

注解

由于历史原因,该函数也接受一个字符串参数,这时该函数就相当于 stripslashes() 的别名。

  • trimslashes($str_)

参数:

  • $str (string) — Input string返回:Slash-trimmed string返回类型:string

移除字符串开头和结尾的所有斜线。例如:

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

注解

该函数已废弃,使用原生的 trim() 函数代替:|| trim($str, '/');

  • reducemultiples($str[, $character = ''[, $trim = FALSE_]])

参数:

  • $str (string) — Text to search in
  • $character (string) — Character to reduce
  • $trim (bool) — Whether to also trim the specified character返回:Reduced string返回类型:string

移除字符串中重复出现的某个指定字符。例如:

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

如果设置第三个参数为 TRUE ,该函数将移除出现在字符串首尾的指定字符。例如:

  1. $string = ",Fred, Bill,, Joe, Jimmy,";
  2. $string = reduce_multiples($string, ", ", TRUE); //results in "Fred, Bill, Joe, Jimmy"
  • quotesto_entities($str_)

参数:

  • $str (string) — Input string返回:String with quotes converted to HTML entities返回类型:string

将字符串中的单引号和双引号转换为相应的 HTML 实体。例如:

  1. $string = "Joe's \"dinner\"";
  2. $string = quotes_to_entities($string); //results in "Joe&#39;s &quot;dinner&quot;"
  • stripquotes($str_)

参数:

  • $str (string) — Input string返回:String with quotes stripped返回类型:string

移除字符串中出现的单引号和双引号。例如:

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