spatie/string

字符串处理已进化

3.0.0 2021-03-30 19:47 UTC

This package is auto-updated.

Last update: 2024-08-30 01:14:40 UTC


README

字符串处理已进化

Latest Version on Packagist Software License Build Status Quality Score Total Downloads

此包提供了一种方便的方法来处理PHP中的字符串。

Spatie是比利时安特卫普的一家网络设计公司。您可以在我们的网站上找到所有开源项目的概述 在这里

支持我们

我们投入了大量资源来创建 一流的开放源代码包。您可以通过 购买我们的付费产品之一 来支持我们。

我们非常感谢您从家乡寄给我们一张明信片,注明您正在使用我们哪个包。您可以在 我们的联系页面 找到我们的地址。我们将所有收到的明信片发布在我们的 虚拟明信片墙上

安装

您可以通过composer安装此包

composer require spatie/string

用法

首先,您必须将原生字符串包装在一个String-object中。这可以通过 string 函数完成。

string('myFirstString');

从那时起,您可以像明天一样连用方法

echo string('StartMiddleEnd')->between('Start', 'End')->toUpper(); // outputs "MIDDLE"

当然,您可以使用我们所有人熟知和喜爱的 . 运算符来拼接输出。

echo 'stuck in the ' . string('StartMiddleEnd')->between('Start', 'End')->toLower() . ' with you';

您还可以使用偏移量来操作字符串。

echo string('hello')[1]->toUpper(); //outputs "E"

$string = string('gray');
$string[2] = 'e';
echo $string->toUpper(); //outputs "GREY"

提供的方法

between

/**
 * Get the string between the given start and end.
 *
 * @param $start
 * @param $end
 * @return \Spatie\String\String
 */
public function between($start, $end)

示例

string('StartMiddleEnd')->between('Start', 'End')->toUpper(); // MIDDLE

toUpper

/**
 * Convert the string to uppercase.
 *
 * @return \Spatie\String\String
 */
public function toUpper()

示例

string('string')->toUpper(); // STRING

toLower

/**
 * Convert the string to lowercase.
 *
 * @return \Spatie\String\String
 */
public function toLower()

示例

string('STRING')->toLower(); // string

tease

/**
 * Shortens a string in a pretty way. It will clean it by trimming
 * it, remove all double spaces and html. If the string is then still
 * longer than the specified $length it will be shortened. The end
 * of the string is always a full word concatinated with the
 * specified moreTextIndicator.
 *
 * @param int $length
 * @param string $moreTextIndicator
 * @return \Spatie\String\String
 */
public function tease($length = 200, $moreTextIndicator = '...')

示例

$longText = 'Now that there is the Tec-9, a crappy spray gun from South Miami. This gun is advertised as the most popular gun in American crime. Do you believe that shit? It actually says that in the little book that comes with it: the most popular gun in American crime.'
string($longText)->tease(10); // Now that...

replaceFirst

/**
 * Replace the first occurrence of a string.
 *
 * @param $search
 * @param $replace
 * @return \Spatie\String\String
 */
public function replaceFirst($search, $replace)

示例

$sentence = 'A good thing is not a good thing.';
string($sentence)->replaceFirst('good', 'bad'); // A bad thing is not a good thing.

replaceLast

/**
 * Replace the last occurrence of a string.
 *
 * @param $search
 * @param $replace
 * @return \Spatie\String\String
 */
public function replaceLast($search, $replace)

示例

$sentence = 'A good thing is not a good thing.';
string($sentence)->replaceLast('good', 'bad'); // A good thing is not a bad thing.

prefix

/**
 * Prefix a string.
 *
 * @param $string
 * @return \Spatie\String\String
 */
public function prefix($string)

示例

string('world')->prefix('hello '); //hello world

suffix

/**
 * Suffix a string.
 *
 * @param $string
 * @return \Spatie\String\String
 */
public function suffix($string)

示例

string('hello')->suffix(' world'); // hello world

concat

这与 suffix 函数相同。

possessive

/**
 * Get the possessive version of a string.
 *
 * @return \Spatie\String\String
 */
public function possessive()

示例

string('Bob')->possessive(); // Bob's
string('Charles')->possessive(); // Charles'

segment

/**
 * Get a segment from a string based on a delimiter.
 * Returns an empty string when the offset doesn't exist.
 * Use a negative index to start counting from the last element.
 * 
 * @param string $delimiter
 * @param int $index
 * 
 * @return \Spatie\String\String
 */
public function segment($delimiter, $index)

相关方法

/**
 * Get the first segment from a string based on a delimiter.
 * 
 * @param string $delimiter
 * 
 * @return \Spatie\String\String
 */
public function firstSegment($delimiter)

/**
 * Get the last segment from a string based on a delimiter.
 * 
 * @param string $delimiter
 * 
 * @return \Spatie\String\String
 */
public function lastSegment($delimiter)

示例

string('foo/bar/baz')->segment('/', 0); // foo
string('foo/bar/baz')->segment('/', 1); // bar
string('foo/bar/baz')->firstSegment('/'); // foo
string('foo/bar/baz')->lastSegment('/'); // baz

pop

/**
 * Pop (remove) the last segment of a string based on a delimiter
 * 
 * @param string $delimiter
 * 
 * @return \Spatie\String\String
 */
public function pop($delimiter)

示例

string('foo/bar/baz')->pop('/'); // foo/bar
string('foo/bar/baz')->pop('/')->pop('/'); // foo

contains

/**
 * Check whether a string contains a substring
 *
 * @param array|string $needle
 * @param bool $caseSensitive
 * @param bool $absolute
 *
 * @return bool
 */
public function contains($delimiter)

示例

string('hello world')->contains('world'); // true
string('hello world')->contains('belgium'); // false

与 underscore.php 的集成

除了上述方法外,您还可以使用 Maxime Fabre的 underscore 包提供的所有字符串方法

例如

string('i am a slug')->slugify()` // returns 'i-am-a-slug'

当然,您可以像我们的方法一样链式调用underscore的方法。

string('i am a slug')->slugify()->between('i', 'a-slug`) // returns '-am-'

请注意,一些underscore方法不返回字符串值。此类方法不可链式调用。

string('freek@spatie.be')->isEmail() // returns true;

测试

您可以使用以下命令运行测试

vendor/bin/phpunit

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

贡献

有关详细信息,请参阅 CONTRIBUTING

安全漏洞

有关如何报告安全漏洞的详细信息,请参阅 我们的安全策略

鸣谢

关于Spatie

Spatie是比利时安特卫普的一家网络设计公司。您可以在我们的网站上找到所有开源项目的概述 在这里

替代方案

此包主要用于在我们的项目中使用。如果您需要一个更全面的字符串包,请查看以下这些

许可证

MIT许可证(MIT)。有关更多信息,请参阅 许可证文件