字符串组件是一组帮助操作字符串的方法。

v1.2.0 2020-09-09 11:32 UTC

This package is auto-updated.

Last update: 2024-09-12 17:27:45 UTC


README

Version License Total downloads Quality Score


安装

使用 Composer

composer require flextype-components/strings

用法

use Flextype\Component\Strings;

方法

方法: Strings::stripSpaces()

从给定字符串中删除所有空白字符。

$string = Strings::stripSpaces('SG-1 returns from an off-world mission');

方法: Strings::trimSlashes()

从一个字符串中删除任何前导和尾随斜杠。

$string = Strings::trimSlashes('some string here/');

方法: Strings::reduceSlashes()

将字符串中的多个斜杠缩减为单个斜杠。

$string = Strings::reduceSlashes('some//text//here');

方法: Strings::stripQuotes()

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

$string = Strings::stripQuotes('some "text" here');

方法: Strings::quotesToEntities()

将单引号和双引号转换为实体。

$string = Strings::quotesToEntities('some "text" here');

方法: Strings::validEncoding()

检查字符串是否在 UTF-8 编码中有效。

$result = Strings::validEncoding('An UTF-8 string here');

方法: Strings::fixEncoding()

从字符串中删除所有无效的 UTF-8 字符。

$string = Strings::fixEncoding('An invalid UTF-8 string here');

方法: Strings::normalizeNewLines()

标准化行结束为类 Unix。

$string = Strings::normalizeNewLines('SG-1 returns from an off-world mission');

方法: Strings::normalizeSpaces()

将空白字符标准化为单个空格。

$string = Strings::normalizeSpaces('SG-1  returns  from  an  off-world  mission');

方法: Strings::random()

// Get random string with predefined settings
$string = Strings::random();

// Get random string with custom length
$string = Strings::random(10);

// Get random string with custom length and custom keyspace
$string = Strings::random(4, '0123456789');

方法: Strings::increment()

向字符串添加 _1 或增加末尾数字以允许 _2_3 等。

// Increment string with predefined settings
$string = Strings::increment('page_1');

// Increment string with custom settings
$string = Strings::increment('page-1', 1, '-');

方法: Strings::wordsCount()

返回字符串中使用的单词信息

// Returns the number of words found
$result = Strings::wordsCount('SG-1 returns from an off-world mission to P9Y-3C3 with Daniel Jackson');

// Returns an array containing all the words found inside the string
$result = Strings::wordsCount('SG-1 returns from an off-world mission to P9Y-3C3 with Daniel Jackson', 1)

// Returns an associative array, where the key is the numeric position of the word inside the string and the value is the actual word itself
$result = Strings::wordsCount('SG-1 returns from an off-world mission to P9Y-3C3 with Daniel Jackson', 2)

方法: Strings::length()

返回给定字符串的长度。

$length = Strings::length('SG-1 returns from an off-world mission to P9Y-3C3');

方法: Strings::lower()

将给定字符串转换为小写。

$string = Strings::lower('SG-1 returns from an off-world mission to P9Y-3C3');

方法: Strings::upper()

将给定字符串转换为大写。

$string = Strings::upper('SG-1 returns from an off-world mission to P9Y-3C3');

方法: Strings::limit()

限制字符串中的字符数。

// Get string with predefined limit settings
$string = Strings::limit('SG-1 returns from an off-world mission to P9Y-3C3');

// Get string with limit 10
$string = Strings::limit('SG-1 returns from an off-world mission to P9Y-3C3', 10);

// Get string with limit 10 and append 'read more...'
$string = Strings::limit('SG-1 returns from an off-world mission to P9Y-3C3', 10, 'read more...');

方法: Strings::studly()

将值转换为 studly caps 格式。

$string = Strings::studly('foo_bar');

方法: Strings::snake()

将字符串转换为 snake case。

$string = Strings::snake('fooBar');

方法: Strings::camel()

将字符串转换为 camel case。

$string = Strings::camel('foo_bar');

方法: Strings::kebab()

将字符串转换为 kebab case。

$string = Strings::kebab('fooBar');

方法: Strings::words()

限制字符串中的单词数。

// Get the number of words in a string with predefined limit settings
$string = Strings::words('SG-1 returns from an off-world mission to P9Y-3C3');

// Get the number of words in a string with limit 3
$string = Strings::words('SG-1 returns from an off-world mission to P9Y-3C3', 3);

// Get the number of words in a string with limit 3 and append 'read more...'
$string = Strings::words('SG-1 returns from an off-world mission to P9Y-3C3', 3, 'read more...');

方法: Strings::contains()

确定给定的字符串是否包含给定的子字符串。

// Determine if a given string contains a given substring.
$result = Strings::contains('SG-1 returns from an off-world mission to P9Y-3C3', 'SG-1');

// Determine if a given string contains a given array of substrings.
$result = Strings::contains('SG-1 returns from an off-world mission to P9Y-3C3', ['SG-1', 'P9Y-3C3']);

方法: Strings::containsAll()

确定给定的字符串是否包含给定的子字符串数组。

$result = Strings::containsAll('SG-1 returns from an off-world mission to P9Y-3C3', ['SG-1', 'P9Y-3C3']);

方法: Strings::containsAny()

确定给定的字符串是否包含数组中的任何值。

$result = Strings::containsAny('SG-1 returns from an off-world mission to P9Y-3C3', ['SG-1', 'P9Y-3C3']);

方法:Strings::substr()

返回由起始和长度参数指定的字符串部分。

// Returns the portion of string specified by the start 0.
$string = Strings::substr('SG-1 returns from an off-world mission to P9Y-3C3', 0);

// Returns the portion of string specified by the start 0 and length 4.
$string = Strings::substr('SG-1 returns from an off-world mission to P9Y-3C3', 0, 4);

方法:Strings::ucfirst()

将字符串的第一个字符转换为大写,其他字符保持不变。

$string = Strings::ucfirst('daniel');

方法:Strings::trim()

从字符串的开始和结束处移除空白(或其他字符)。

$string = Strings::trim(' daniel ');

方法:Strings::trimRight()

从字符串的末尾移除空白(或其他字符)。

$string = Strings::trimRight('daniel ');

方法:Strings::trimLeft()

从字符串的开始移除空白(或其他字符)。

$string = Strings::trimLeft(' daniel');

方法:Strings::capitalize()

将字符串中每个单词的第一个字符转换为大写,其他字符转换为小写。

$string = Strings::capitalize('that country was at the same stage of development as the United States in the 1940s');

方法:Strings::reverse()

反转字符串。

$string = Strings::reverse('SG-1 returns from an off-world mission');

方法:Strings::segments()

基于分隔符从字符串中获取段数组。

// Get array of segments from a string based on a predefined delimiter.
$segments = Strings::segments('SG-1 returns from an off-world mission');

// Get array of segments from a string based on a delimiter '-'.
$segments = Strings::segments('SG-1 returns from an off-world mission', '-');

方法:Strings::segment()

基于分隔符从字符串中获取一个段。如果偏移量不存在,则返回空字符串。使用负索引从最后一个元素开始计数。

// Get a segment 1 from a string based on a predefined delimiter.
$string = Strings::segment('SG-1 returns from an off-world mission', 1);

// Get a segment 1 from a string based on a delimiter '-'.
$string = Strings::segment('SG-1 returns from an off-world mission', 1, '-');

// Get a segment 1 from a string starting from the last based on a delimiter '-'.
$string = Strings::segment('SG-1 returns from an off-world mission', -1, '-');

方法:Strings::firstSegment()

基于分隔符从字符串中获取第一个段。

// Get a first segment from a string based on a predefined delimiter.
$string = Strings::firstSegment('SG-1 returns from an off-world mission');

// Get a first segment from a string based on a delimiter '-'.
$string = Strings::firstSegment('SG-1 returns from an off-world mission', '-');

方法:Strings::lastSegment()

基于分隔符从字符串中获取最后一个段。

// Get a last segment from a string based on a predefined delimiter.
$string = Strings::lastSegment('SG-1 returns from an off-world mission');

// Get a last segment from a string based on a delimiter '-'.
$string = Strings::lastSegment('SG-1 returns from an off-world mission', '-');

方法:Strings::between()

获取给定两个值之间的字符串部分。

$string = Strings::between('SG-1 returns from an off-world mission', 'SG-1', 'from');

方法:Strings::before()

获取给定值首次出现之前的部分。

$string = Strings::before('SG-1 returns from an off-world mission', 'mission');

方法:Strings::beforeLast()

获取给定值最后出现之前的部分。

$string = Strings::beforeLast('SG-1 returns from an off-world mission', 'mission');

方法:Strings::after()

返回给定值首次出现之后的部分。

$string = Strings::after('SG-1 returns from an off-world mission', 'SG-1');

方法:Strings::afterLast()

返回给定值最后出现之后的部分。

$string = Strings::afterLast('SG-1 returns from an off-world mission', 'SG-1');

方法:Strings::padBoth()

用另一个字符串填充字符串的两边。

$string = Strings::padBoth('SG-1 returns from an off-world mission', 50, '-');

方法:Strings::padRight()

用另一个字符串填充字符串的右边。

$string = Strings::padRight('SG-1 returns from an off-world mission', 50, '-');

方法:Strings::padLeft()

用另一个字符串填充字符串的左边。

$string = Strings::padLeft('SG-1 returns from an off-world mission', 50, '-');

方法:Strings::replaceArray()

使用数组逐个替换字符串中的给定值。

$string = Strings::replaceArray('SG-1 returns from an off-world mission', 'SG-1', ['SG-2']);

方法:Strings::replaceFirst()

替换字符串中给定值首次出现的位置。

$string = Strings::replaceFirst('SG-1 returns from an off-world mission', 'SG-1', 'SG-2');

方法:Strings::replaceLast()

替换字符串中给定值最后出现的位置。

$string = Strings::replaceLast('SG-1 returns from an off-world mission', 'off-world', 'P9Y-3C3');

方法:Strings::start()

在字符串开始处添加一个给定值的实例。

$string = Strings::start('movies/sg-1/season-5/episode-21/', '/');

方法:Strings::startsWith()

确定给定字符串是否以给定子字符串开头。

$result = Strings::startsWith('/movies/sg-1/season-5/episode-21/', '/');

方法:Strings::endsWith()

确定给定字符串是否以给定子字符串结尾。

$result = Strings::endsWith('/movies/sg-1/season-5/episode-21/', '/');

方法:Strings::finish()

给字符串添加给定值的单个实例。

$result = Strings::finish('/movies/sg-1/season-5/episode-21', '/');

方法:Strings::hash()

从输入字符串生成哈希字符串。

// Get string hash with predefined settings
$result = Strings::hash('SG-1 returns from an off-world mission');

// Get string hash with hashed with sha256 algorithm
$result = Strings::hash('SG-1 returns from an off-world mission', 'sha256');

// Get string hash with hashed with sha256 algorithm and with raw output
$result = Strings::hash('SG-1 returns from an off-world mission', 'sha256', true);

许可证

MIT许可证(MIT) 版权(c)2020 Sergey Romanenko