文本辅助函数

文本辅助函数文件包含帮助处理文本的函数。

加载此辅助函数

使用以下代码加载此辅助函数:

<?php

helper('text');

可用函数

以下函数可用:

random_string([$type = 'alnum'[, $len = 8]])
参数:
  • $type (string) – 随机化类型

  • $len (int) – 输出字符串长度

返回:

随机字符串

返回类型:

string

根据你指定的类型和长度生成随机字符串。用于创建密码或生成随机哈希值非常有用。

警告

对于类型: basicmd5sha1,生成的字符串在加密上不是安全的。因此,这些类型不能用于加密目的或需要不可猜测返回值的目的。从 v4.3.3 开始,这些类型已弃用。

第一个参数指定字符串的类型,第二个参数指定长度。可用的选择有:

  • alpha:仅包含小写和大写字母的字符串。

  • alnum:包含小写和大写字符的字母数字字符串。

  • basic:[已弃用]基于 mt_rand() 的随机数(忽略长度)。

  • numeric:数字字符串。

  • nozero:不包含零的数字字符串。

  • md5:[已弃用]基于 md5() 的加密随机数(固定长度 32)。

  • sha1:[已弃用]基于 sha1() 的加密随机数(固定长度 40)。

  • crypto:基于 random_bytes() 的随机字符串。

备注

当你使用 crypto 时,必须为第二个参数设置为偶数。从 v4.2.2 开始,如果你设置为奇数,将抛出 InvalidArgumentException

备注

从 v4.3.3 开始, alphaalnumnozero 使用 random_byte(), numeric 使用 random_int()。在以前的版本中,它使用在加密上不安全的 str_shuffle()

使用示例:

<?php

echo random_string('alnum', 16);
increment_string($str[, $separator = '_'[, $first = 1]])
参数:
  • $str (string) – 输入字符串

  • $separator (string) – 要与重复数字一起追加的分隔符

  • $first (int) – 起始数字

返回:

增量字符串

返回类型:

string

通过追加一个数字或增加数字来增加字符串。用于创建文件或数据库内容的“副本”非常有用,这些内容具有唯一的标题或 slug。

使用示例:

<?php

echo increment_string('file', '_'); // "file_1"
echo increment_string('file', '-', 2); // "file-2"
echo increment_string('file_4'); // "file_5"
alternator($args)
参数:
  • $args (mixed) – 可变数量的参数

返回:

交替的字符串

返回类型:

mixed

在循环中允许在两个或多个项目之间交替。示例:

<?php

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

你可以添加尽可能多的参数,并且在每个循环迭代中将返回下一个项目。

<?php

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

备注

要使用对此函数的多个单独调用,只需不带参数调用该函数即可重新初始化。

reduce_double_slashes($str)
参数:
  • $str (string) – 输入字符串

返回:

正规化斜杠的字符串

返回类型:

string

将字符串中紧邻出现的多个斜杠转换为单个斜杠,但不包括那些在 URL 协议前缀中找到的斜杠(例如 http&#58;//)。

示例:

<?php

$string = 'http://example.com//index.php';
echo reduce_double_slashes($string); // results in "http://example.com/index.php"
strip_slashes($data)
参数:
  • $data (mixed) – 输入字符串或字符串数组

返回:

斜杠被剥离的字符串

返回类型:

mixed

从字符串数组中删除任何斜杠。

示例:

<?php

$str = [
    'question' => "Is your name O\\'reilly?",
    'answer'   => "No, my name is O\\'connor.",
];

$str = strip_slashes($str);

以上将返回以下数组:

<?php

[
    'question' => "Is your name O'reilly?",
    'answer'   => "No, my name is O'connor.",
];

备注

出于历史原因,此函数也接受并处理字符串输入。但是,这使它只是一个 stripslashes() 的别名。

reduce_multiples($str[, $character = ''[, $trim = false]])
参数:
  • $str (string) – 要搜索的文本

  • $character (string) – 要缩减的字符

  • $trim (bool) – 是否也去除指定的字符

返回:

缩减后的字符串

返回类型:

string

直接相继出现多个特定字符时,减少其出现次数。示例:

<?php

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

如果第三个参数设置为 true,则会移除字符串开头和结尾处的字符出现。示例:

<?php

$string = ',Fred, Bill,, Joe, Jimmy,';
$string = reduce_multiples($string, ', ', true); // results in "Fred, Bill, Joe, Jimmy"
quotes_to_entities($str)
参数:
  • $str (string) – 输入字符串

返回:

引号转换为 HTML 实体的字符串

返回类型:

string

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

<?php

$string = "Joe's \"dinner\"";
$string = quotes_to_entities($string); // results in "Joe&#39;s &quot;dinner&quot;"
strip_quotes($str)
参数:
  • $str (string) – 输入字符串

返回:

不带引号的字符串

返回类型:

string

从字符串中删除单引号和双引号。示例:

<?php

$string = "Joe's \"dinner\"";
$string = strip_quotes($string); // results in "Joes dinner"
word_limiter($str[, $limit = 100[, $end_char = '&#8230;']])
参数:
  • $str (string) – 输入字符串

  • $limit (int) – 限制

  • $end_char (string) – 结束字符(通常是省略号)

返回:

限制字数的字符串

返回类型:

string

将字符串截断为指定的 词数。示例:

<?php

$string = 'Here is a nice text string consisting of eleven words.';
$string = word_limiter($string, 4);
// Returns:  Here is a nice

第三个参数是一个可选的后缀,添加到字符串末尾。默认它添加一个省略号。

character_limiter($str[, $n = 500[, $end_char = '&#8230;']])
参数:
  • $str (string) – 输入字符串

  • $n (int) – 字符数

  • $end_char (string) – 结束字符(通常是省略号)

返回:

限制字符数的字符串

返回类型:

string

将字符串截断为指定的 字符数。它保持词的完整性,所以字符数量可能略多于或略少于你指定的数量。

示例:

<?php

$string = 'Here is a nice text string consisting of eleven words.';
$string = character_limiter($string, 20);
// Returns:  Here is a nice text string

第三个参数是一个可选的后缀,如果未声明则此辅助函数使用省略号。

备注

如果你需要截断为确切的字符数,请参见下面的 ellipsize() 函数。

ascii_to_entities($str)
参数:
  • $str (string) – 输入字符串

返回:

ASCII 值转换为实体的字符串

返回类型:

string

将 ASCII 值转换为字符实体,包括可能在网页中造成问题的高 ASCII 和 MS Word 字符,以便它们可以与浏览器设置无关地一致显示,或者可靠地存储在数据库中。这在一定程度上取决于服务器支持的字符集,所以在所有情况下都可能不完全可靠,但在大多数情况下,它应该正确识别正常范围之外的字符(如带重音的字符)。

示例:

<?php

$string = ascii_to_entities($string);
entities_to_ascii($str[, $all = true])
参数:
  • $str (string) – 输入字符串

  • $all (bool) – 是否也转换不安全的实体

返回:

HTML 实体转换为 ASCII 字符的字符串

返回类型:

string

此函数与 ascii_to_entities() 相反。它将字符实体转换回 ASCII。

convert_accented_characters($str)
参数:
  • $str (string) – 输入字符串

返回:

重音字符转换后的字符串

返回类型:

string

将高位 ASCII 字符转换为低位 ASCII 等效字符。在只能安全使用标准 ASCII 字符的地方需要非英语字符时很有用,例如 URL 中。

示例:

<?php

$string = convert_accented_characters($string);

备注

此函数使用配套的配置文件 app/Config/ForeignCharacters.php 来定义音译的 to 和 from 数组。

word_censor($str, $censored[, $replacement = ''])
参数:
  • $str (string) – 输入字符串

  • $censored (array) – 要审查的标记词列表

  • $replacement (string) – 用什么替换标记词

返回:

审查后的字符串

返回类型:

string

使你能够审查文本字符串中的词。第一个参数将包含原始字符串。第二个将包含你不允许的词数组。第三个(可选)参数可以包含标记词的替换值。如果未指定,它们将被 #### 替换。

示例:

<?php

$disallowed = ['darn', 'shucks', 'golly', 'phooey'];
$string     = word_censor($string, $disallowed, 'Beep!');
highlight_code($str)
参数:
  • $str (string) – 输入字符串

返回:

通过 HTML 着色的字符串

返回类型:

string

使用 HTML 对代码字符串(PHP、HTML等)上色。示例:

<?php

$string = highlight_code($string);

此函数使用 PHP 的 highlight_string() 函数,因此使用的颜色是在 php.ini 文件中指定的颜色。

highlight_phrase($str, $phrase[, $tag_open = '<mark>'[, $tag_close = '</mark>']])
参数:
  • $str (string) – 输入字符串

  • $phrase (string) – 要突出显示的短语

  • $tag_open (string) – 用于突出显示的开标签

  • $tag_close (string) – 突出显示的闭标签

返回:

通过 HTML 突出显示短语的字符串

返回类型:

string

将文本字符串中的短语突出显示。第一个参数将包含原始字符串,第二个将包含你希望突出显示的短语。第三和第四个参数将包含你希望短语包裹在里面的开启/关闭 HTML 标记。

示例:

<?php

$string = 'Here is a nice text string about nothing in particular.';
echo highlight_phrase($string, 'nice text', '<span style="color:#990000;">', '</span>');

以上代码打印:

Here is a <span style="color:#990000;">nice text</span> string about nothing in particular.

备注

此函数过去默认使用 <strong> 标签。旧版浏览器可能不支持新的 HTML5 mark 标签,因此如果你需要支持这样的浏览器, 建议你将以下 CSS 代码插入样式表中:

mark {
    background: #ff0;
    color: #000;
};
word_wrap($str[, $charlim = 76])
参数:
  • $str (string) – 输入字符串

  • $charlim (int) – 字符限制

返回:

自动换行的字符串

返回类型:

string

在保持完整单词的同时,在指定的 字符数 处换行文本。

示例:

<?php

$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.
 *
 * Excessively long words will be split, but URLs will not be.
 */
ellipsize($str, $max_length[, $position = 1[, $ellipsis = '&hellip;']])
参数:
  • $str (string) – 输入字符串

  • $max_length (int) – 字符串长度限制

  • $position (mixed) – 拆分位置(整数或浮点数)

  • $ellipsis (string) – 要用作省略号的字符

返回:

省略的字符串

返回类型:

string

此函数将从字符串中剥离标签,在定义的最大长度处对其进行拆分,并在前后插入省略号。

第一个参数是要提取摘录的字符串,第二个是最终字符串中的字符数。第三个参数是省略号应出现的字符串中的位置,从 0 到 1,从左到右。例如。值为 1 将省略号放在字符串的右边,.5 在中间,0 在左边。

可选的第四个参数是省略号的类型。默认情况下,将插入 &hellip;。

示例:

<?php

$str = 'this_string_is_entirely_too_long_and_might_break_my_design.jpg';
echo ellipsize($str, 32, .5);

产生:

this_string_is_e&hellip;ak_my_design.jpg
excerpt($text, $phrase = false, $radius = 100, $ellipsis = '...')
参数:
  • $text (string) – 要提取摘录的文本

  • $phrase (string) – 要围绕其提取文本的短语或单词

  • $radius (int) – $phrase 前后的字符数

  • $ellipsis (string) – 要用作省略号的字符

返回:

摘录。

返回类型:

string

此函数将在中心短语 $phrase 前后提取 $radius 个字符,在前后都有省略号。

第一个参数是要提取摘录的文本,第二个是计数前后中间的单词或短语。第三个参数是中央短语前后要计数的字符数。如果没有传递短语,摘录将包括第一个 $radius 个字符,以及末尾的省略号。

示例:

<?php

$text = 'Ut vel faucibus odio. Quisque quis congue libero. Etiam gravida
eros lorem, eget porttitor augue dignissim tincidunt. In eget risus eget
mauris faucibus molestie vitae ultricies odio. Vestibulum id ultricies diam.
Curabitur non mauris lectus. Phasellus eu sodales sem. Integer dictum purus
ac enim hendrerit gravida. Donec ac magna vel nunc tincidunt molestie sed
vitae nisl. Cras sed auctor mauris, non dictum tortor. Nulla vel scelerisque
arcu. Cras ac ipsum sit amet augue laoreet laoreet. Aenean a risus lacus.
Sed ut tortor diam.';

echo excerpt($text, 'Donec');

产生:

... non mauris lectus. Phasellus eu sodales sem. Integer dictum purus ac
enim hendrerit gravida. Donec ac magna vel nunc tincidunt molestie sed
vitae nisl. Cras sed auctor mauris, non dictum tortor. ...