CakePHP
  • 文档
    • 书籍
    • API
    • 视频
    • 报告安全问题
    • 隐私政策
    • 标识 & 商标
  • 商业解决方案
  • 周边产品
  • 公路旅行
  • 团队
  • 社区
    • 社区
    • 参与进来
    • 问题 (Github)
    • Bakery
    • 精选资源
    • 培训
    • 聚会
    • 我的 CakePHP
    • CakeFest
    • 新闻稿
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • 帮助 & 支持
    • 论坛
    • Stack Overflow
    • IRC
    • Slack
    • 付费支持
CakePHP

C CakePHP 5.1 Chiffon API

  • 项目
    • CakePHP
      • CakePHP
      • Chronos
      • Elastic Search
      • 队列
  • 版本
    • 5.1
      • 5.1
      • 5.0
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

命名空间

  • 全局
  • Cake
    • 缓存
    • 集合
    • 命令
    • 控制台
    • 控制器
    • 核心
    • 数据库
    • 数据源
    • 错误
    • 事件
    • 表单
    • HTTP
    • 国际化
    • 日志
    • 邮件发送器
    • 网络
    • ORM
    • 路由
    • 测试套件
    • 实用程序
      • 加密
      • 异常
    • 验证
    • 视图

类 Text

文本处理方法。

命名空间: Cake\Utility

属性概述

  • $_defaultHtmlNoCount protected static
    list<string>

    用于截断文本时不应计算的默认 HTML 标签。

  • $_defaultTransliterator protected static
    Transliterator|null

    默认的音译器。

  • $_defaultTransliteratorId protected static
    string

    默认的音译器 ID 字符串。

方法概述

  • _removeLastWord() protected static

    从输入文本中删除最后一个单词。

  • _strlen() protected static

    获取字符串长度。

  • _substr() protected static

    返回字符串的一部分。

  • _wordWrap() protected static

    作为辅助方法的 Unicode 版本的 wordwrap。

  • ascii() public static

    将多字节字符字符串的十进制值转换为字符串

  • cleanInsert() public static

    使用给定的 $options 清理 Text::insert() 格式化的字符串,具体取决于 $options 中的 'clean' 键。使用的默认方法是 text,但也可以使用 html。此函数的目标是替换所有空格和占位符周围不需要的标记,这些占位符没有被 Text::insert() 替换。

  • excerpt() public static

    从文本中提取短语周围的摘录,在短语两侧的字符数量由半径确定。

  • getTransliterator() public static

    获取默认的音译器。

  • getTransliteratorId() public static

    获取默认的音译器标识符字符串。

  • highlight() public static

    在文本中突出显示给定的短语。您可以在高亮显示器中指定任何表达式,该表达式可能包括 \1 表达式以包含找到的 $phrase。

  • insert() public static

    用任何给定的 $data 替换 $str 中的变量占位符。$data 数组中的每个键对应于 $str 中的变量占位符名称。例如

    Text::insert(':name is :age years old.', ['name' => 'Bob', 'age' => '65']);

    返回值:Bob 今年 65 岁。

  • isMultibyte() public static

    检查字符串是否包含多字节字符

  • parseFileSize() public static

    将文件大小从人类可读字符串转换为字节

  • setTransliterator() public static

    设置默认的音译器。

  • setTransliteratorId() public static

    设置默认的音译器标识符字符串。

  • slug() public static

    返回一个字符串,其中所有空格都转换为连字符(默认情况下),字符被音译为 ASCII 字符,并且非单词字符被移除。

  • tail() public static

    从末尾截断文本。

  • toList() public static

    创建一个逗号分隔的列表,其中最后两项用 'and' 连接,形成自然语言。

  • tokenize() public static

    使用 $separator 对字符串进行标记化,忽略 $leftBound 和 $rightBound 之间出现的 $separator 的任何实例。

  • transliterate() public static

    音译字符串。

  • truncate() public static

    截断文本。

  • truncateByWidth() public static

    使用指定的宽度截断文本。

  • utf8() public static

    将多字节字符字符串转换为字符的十进制值

  • uuid() public static

    生成一个随机的 UUID 版本 4

  • wordWrap() public static

    Unicode 和换行符感知版本的 wordwrap。

  • wrap() public static

    将文本包装到特定宽度,可以选择在单词断点处包装。

  • wrapBlock() public static

    将完整的文本块包装到特定宽度,可以选择在单词断点处包装。

方法详细信息

_removeLastWord() ¶ protected static

_removeLastWord(string $text): string

从输入文本中删除最后一个单词。

参数
string $text

输入文本

返回值
string

_strlen() ¶ protected static

_strlen(string $text, array<string, mixed> $options): int

获取字符串长度。

选项

  • html 如果为真,则 HTML 实体将被视为解码的字符。
  • trimWidth 如果为真,则将返回宽度。
参数
string $text

正在检查长度的字符串

array<string, mixed> $options

一个选项数组。

返回值
int

_substr() ¶ protected static

_substr(string $text, int $start, int|null $length, array<string, mixed> $options): string

返回字符串的一部分。

选项

  • html 如果为真,则 HTML 实体将被视为解码的字符。
  • trimWidth 如果为真,则将使用指定的宽度截断。
参数
string $text

输入字符串。

int $start

开始提取的位置。

int|null $length

所需的长度。

array<string, mixed> $options

一个选项数组。

返回值
string

_wordWrap() ¶ protected static

_wordWrap(string $text, int $width = 72, string $break = "\n", bool $cut = false): string

作为辅助方法的 Unicode 版本的 wordwrap。

参数
string $text

要格式化的文本。

int $width optional

要包装到的宽度。默认为 72。

string $break optional

使用可选的 break 参数来中断行。默认为 '\n'。

bool $cut optional

如果 cut 设置为 true,则字符串始终在指定的宽度处包装。

返回值
string

ascii() ¶ public static

ascii(array<int> $array): string

将多字节字符字符串的十进制值转换为字符串

参数
array<int> $array

数组

返回值
string

cleanInsert() ¶ public static

cleanInsert(string $str, array<string, mixed> $options): string

使用给定的 $options 清理 Text::insert() 格式化的字符串,具体取决于 $options 中的 'clean' 键。使用的默认方法是 text,但也可以使用 html。此函数的目标是替换所有空格和占位符周围不需要的标记,这些占位符没有被 Text::insert() 替换。

参数
string $str

要清理的字符串。

array<string, mixed> $options

选项列表。

返回值
string
另见
\Cake\Utility\Text::insert()

excerpt() ¶ public static

excerpt(string $text, string $phrase, int $radius = 100, string $ellipsis = '…'): string

从文本中提取短语周围的摘录,在短语两侧的字符数量由半径确定。

参数
string $text

要搜索短语的字符串

string $phrase

将要搜索的短语

int $radius optional

将在找到的短语两侧返回的字符数量

string $ellipsis optional

将要追加的结尾

返回值
string
链接
https://book.cakephp.com.cn/5/en/core-libraries/text.html#extracting-an-excerpt

getTransliterator() ¶ public static

getTransliterator(): Transliterator|null

获取默认的音译器。

返回值
Transliterator|null

getTransliteratorId() ¶ public static

getTransliteratorId(): string

获取默认的音译器标识符字符串。

返回值
string

highlight() ¶ public static

highlight(string $text, list<string>|string $phrase, array<string, mixed> $options = []): string

在文本中突出显示给定的短语。您可以在高亮显示器中指定任何表达式,该表达式可能包括 \1 表达式以包含找到的 $phrase。

选项

  • format 使用该短语突出显示的 HTML 片段
  • html 如果为真,则将忽略任何 HTML 标签,确保仅突出显示正确的文本
  • regex 用于匹配单词的自定义正则表达式规则,默认值为 '|$tag|iu'
  • limit 限制,可选,默认为 -1(无)
参数
string $text

要搜索短语的文本。

list<string>|string $phrase

将要搜索的短语或短语。

array<string, mixed> $options optional

一个 HTML 属性和选项数组。

返回值
string
链接
https://book.cakephp.com.cn/5/en/core-libraries/text.html#highlighting-substrings

insert() ¶ public static

insert(string $str, array $data, array<string, mixed> $options = []): string

用任何给定的 $data 替换 $str 中的变量占位符。$data 数组中的每个键对应于 $str 中的变量占位符名称。例如

Text::insert(':name is :age years old.', ['name' => 'Bob', 'age' => '65']);

返回值:Bob 今年 65 岁。

可用的 $options 是

  • before: 变量占位符名称前面的字符或字符串(默认值为 :)
  • after: 变量占位符名称后面的字符或字符串(默认值为 null)
  • escape: 用于转义 before 字符/字符串的字符或字符串(默认值为 \)
  • format: 用于匹配变量占位符的正则表达式。默认值为:/(?<!\\)\:%s/(覆盖 before、after,打破转义/清理)
  • clean: 布尔值或包含 Text::cleanInsert 指令的数组
参数
string $str

包含变量占位符的字符串

array $data

一个键 => 值数组,其中每个键代表一个占位符变量名称,将被替换为值

array<string, mixed> $options optional

一个选项数组,参见上面的描述

返回值
string

isMultibyte() ¶ public static

isMultibyte(string $string): bool

检查字符串是否包含多字节字符

参数
string $string

要测试的值

返回值
bool

parseFileSize() ¶ public static

parseFileSize(string $size, mixed $default = false): mixed

将文件大小从人类可读字符串转换为字节

参数
string $size

人类可读的字符串格式的大小,例如 '5MB'、'5M'、'500B'、'50kb' 等。

mixed $default 可选

当使用无效大小时要返回的值,例如 'Unknown type'

返回值
mixed
抛出
InvalidArgumentException
在无效的单位类型上。
链接
https://book.cakephp.com.cn/5/en/core-libraries/text.html#Cake\Utility\Text::parseFileSize

setTransliterator() ¶ public static

setTransliterator(Transliterator $transliterator): void

设置默认的音译器。

参数
Transliterator $transliterator

一个 Transliterator 实例。

返回值
void

setTransliteratorId() ¶ public static

setTransliteratorId(string $transliteratorId): void

设置默认的音译器标识符字符串。

参数
string $transliteratorId

转写标识符。

返回值
void

slug() ¶ public static

slug(string $string, array<string, mixed>|string $options = []): string

返回一个字符串,其中所有空格都转换为连字符(默认情况下),字符被音译为 ASCII 字符,并且非单词字符被移除。

选项

  • replacement: 替换字符串。默认值为 '-'.
  • transliteratorId: 有效的转写标识符字符串。如果为 null(默认值),则将使用通过 setTransliteratorId() 或 setTransliterator() 设置的转写器(标识符)。如果为 false,则不会进行转写,只会删除非单词字符。
  • preserve: 要保留的特定非单词字符。默认值为 null。例如,此选项可以设置为 '.' 以生成干净的文件名。
参数
string $string

要转换的字符串

array<string, mixed>|string $options 可选

如果为字符串,它将用作替换字符,或用作选项数组。

返回值
string
另见
setTransliterator()
setTransliteratorId()

tail() ¶ public static

tail(string $text, int $length = 100, array<string, mixed> $options = []): string

从末尾截断文本。

将字符串截断为 $length 的长度,如果文本长度超过 length,则用省略号替换前几个字符。

选项

  • ellipsis 将用作开头,并附加到截断的字符串之前
  • exact 如果为 false,则不会在单词中间截断 $text
参数
string $text

要截断的字符串。

int $length 可选

返回字符串的长度,包括省略号。

array<string, mixed> $options optional

一个选项数组。

返回值
string

toList() ¶ public static

toList(list<string> $list, string|null $and = null, string $separator = ', '): string

创建一个逗号分隔的列表,其中最后两项用 'and' 连接,形成自然语言。

参数
list<string> $list

要连接的列表。

string|null $and 可选

用于将最后和倒数第二个项目连接在一起的词语。默认值为 'and'。

string $separator 可选

用于将所有其他项目连接在一起的分隔符。默认值为 ', '。

返回值
string
链接
https://book.cakephp.com.cn/5/en/core-libraries/text.html#converting-an-array-to-sentence-form

tokenize() ¶ public static

tokenize(string $data, string $separator = ',', string $leftBound = '(', string $rightBound = ')'): list<string>

使用 $separator 对字符串进行标记化,忽略 $leftBound 和 $rightBound 之间出现的 $separator 的任何实例。

参数
string $data

要标记化的数据。

string $separator 可选

用于分割数据的标记。

string $leftBound 可选

要忽略分隔符的左边界。

string $rightBound 可选

要忽略分隔符的右边界。

返回值
list<string>

transliterate() ¶ public static

transliterate(string $string, Transliterator|string|null $transliterator = null): string

音译字符串。

参数
string $string

要转写的字符串。

Transliterator|string|null $transliterator 可选

一个 Transliterator 实例,或一个转写器标识符字符串。如果为 null,则将使用通过 setTransliteratorId() 或 setTransliterator() 设置的默认转写器(标识符)。

返回值
string
另见
https://secure.php.net/manual/en/transliterator.transliterate.php

truncate() ¶ public static

truncate(string $text, int $length = 100, array<string, mixed> $options = []): string

截断文本。

将字符串截断为 $length 的长度,如果文本长度超过 length,则用省略号替换最后几个字符。

选项

  • ellipsis 将用作结尾,并附加到截断的字符串之后
  • exact 如果为 false,则不会在单词中间截断 $text
  • html 如果为 true,则会正确处理 HTML 标签
  • trimWidth 如果为 true,则 $text 将以宽度截断
参数
string $text

要截断的字符串。

int $length 可选

返回字符串的长度,包括省略号。

array<string, mixed> $options optional

一个 HTML 属性和选项数组。

返回值
string
链接
https://book.cakephp.com.cn/5/en/core-libraries/text.html#truncating-text

truncateByWidth() ¶ public static

truncateByWidth(string $text, int $length = 100, array<string, mixed> $options = []): string

使用指定的宽度截断文本。

参数
string $text

要截断的字符串。

int $length 可选

返回字符串的长度,包括省略号。

array<string, mixed> $options optional

一个 HTML 属性和选项数组。

返回值
string
另见
\Cake\Utility\Text::truncate()

utf8() ¶ public static

utf8(string $string): array<int>

将多字节字符字符串转换为字符的十进制值

参数
string $string

要转换的字符串。

返回值
array<int>

uuid() ¶ public static

uuid(): string

生成一个随机的 UUID 版本 4

警告:此方法不应作为任何加密操作的随机种子使用。相反,您应该使用 Security::randomBytes() 或 Security::randomString()。

它也不应用于创建具有安全隐患的标识符,例如 "不可猜测" 的 URL 标识符。相反,您应该使用 {@link \Cake\Utility\Security::randomBytes()}` 来完成此操作。

返回值
string
另见
https://www.ietf.org/rfc/rfc4122.txt

wordWrap() ¶ public static

wordWrap(string $text, int $width = 72, string $break = "\n", bool $cut = false): string

Unicode 和换行符感知版本的 wordwrap。

参数
string $text

要格式化的文本。

int $width optional

要包装到的宽度。默认为 72。

string $break optional

使用可选的 break 参数来中断行。默认为 '\n'。

bool $cut optional

如果 cut 设置为 true,则字符串始终在指定的宽度处包装。

返回值
string

wrap() ¶ public static

wrap(string $text, array<string, mixed>|int $options = []): string

将文本包装到特定宽度,可以选择在单词断点处包装。

选项

  • width 要换行的宽度。默认值为 72。
  • wordWrap 仅在单词断开处(空格)换行。默认值为 true。
  • indent 用来缩进的字符串。默认值为 null。
  • indentAt 从 0 开始的索引,从该索引开始缩进。默认值为 0。
参数
string $text

要格式化的文本。

array<string, mixed>|int $options 可选

要使用的选项数组,或用于将文本换行的整数。

返回值
string

wrapBlock() ¶ public static

wrapBlock(string $text, array<string, mixed>|int $options = []): string

将完整的文本块包装到特定宽度,可以选择在单词断点处包装。

选项

  • width 要换行的宽度。默认值为 72。
  • wordWrap 仅在单词断开处(空格)换行。默认值为 true。
  • indent 用来缩进的字符串。默认值为 null。
  • indentAt 从 0 开始的索引,从该索引开始缩进。默认值为 0。
参数
string $text

要格式化的文本。

array<string, mixed>|int $options 可选

要使用的选项数组,或用于将文本换行的整数。

返回值
string

属性详情

$_defaultHtmlNoCount ¶ protected static

用于截断文本时不应计算的默认 HTML 标签。

类型
list<string>

$_defaultTransliterator ¶ protected static

默认的音译器。

类型
Transliterator|null

$_defaultTransliteratorId ¶ protected static

默认的音译器 ID 字符串。

类型
string
OpenHub
Pingping
Linode
  • 商业解决方案
  • 展示
  • 文档
  • 书籍
  • API
  • 视频
  • 报告安全问题
  • 隐私政策
  • 标识 & 商标
  • 社区
  • 参与进来
  • 问题 (Github)
  • Bakery
  • 精选资源
  • 培训
  • 聚会
  • 我的 CakePHP
  • CakeFest
  • 新闻稿
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • 帮助 & 支持
  • 论坛
  • Stack Overflow
  • IRC
  • Slack
  • 付费支持

使用 CakePHP API 文档 生成