Skip to content

字符串

简介

Laravel 包含多种操作字符串值的函数。其中许多函数由框架本身使用;但是,如果您觉得方便,可以在自己的应用程序中自由使用它们。

可用方法

字符串

流畅字符串

字符串

__()

__ 函数使用您的语言文件翻译给定的翻译字符串或翻译键:

php
echo __('Welcome to our application');

echo __('messages.welcome');

如果指定的翻译字符串或键不存在,__ 函数将返回给定的值。因此,使用上面的示例,如果该翻译键不存在,__ 函数将返回 messages.welcome

class_basename()

class_basename 函数返回给定类的类名,并去除类的命名空间:

php
$class = class_basename('Foo\Bar\Baz');

// Baz

e()

e 函数运行 PHP 的 htmlspecialchars 函数,默认将 double_encode 选项设置为 true

php
echo e('<html>foo</html>');

// &lt;html&gt;foo&lt;/html&gt;

preg_replace_array()

preg_replace_array 函数使用数组按顺序替换字符串中的给定模式:

php
$string = 'The event will take place between :start and :end';

$replaced = preg_replace_array('/:[a-z_]+/', ['8:30', '9:00'], $string);

// The event will take place between 8:30 and 9:00

Str::after()

Str::after 方法返回字符串中给定值之后的所有内容。如果该值在字符串中不存在,则返回整个字符串:

php
use Illuminate\Support\Str;

$slice = Str::after('This is my name', 'This is');

// ' my name'

Str::afterLast()

Str::afterLast 方法返回字符串中给定值最后一次出现之后的所有内容。如果该值在字符串中不存在,则返回整个字符串:

php
use Illuminate\Support\Str;

$slice = Str::afterLast('App\Http\Controllers\Controller', '\\');

// 'Controller'

Str::apa()

Str::apa 方法按照 APA 指南将给定字符串转换为标题大小写:

php
use Illuminate\Support\Str;

$title = Str::apa('Creating A Project');

// 'Creating a Project'

Str::ascii()

Str::ascii 方法将尝试将字符串音译为 ASCII 值:

php
use Illuminate\Support\Str;

$slice = Str::ascii('û');

// 'u'

Str::before()

Str::before 方法返回字符串中给定值之前的所有内容:

php
use Illuminate\Support\Str;

$slice = Str::before('This is my name', 'my name');

// 'This is '

Str::beforeLast()

Str::beforeLast 方法返回字符串中给定值最后一次出现之前的所有内容:

php
use Illuminate\Support\Str;

$slice = Str::beforeLast('This is my name', 'is');

// 'This '

Str::between()

Str::between 方法返回字符串中两个值之间的部分:

php
use Illuminate\Support\Str;

$slice = Str::between('This is my name', 'This', 'name');

// ' is my '

Str::betweenFirst()

Str::betweenFirst 方法返回字符串中两个值之间的最小可能部分:

php
use Illuminate\Support\Str;

$slice = Str::betweenFirst('[a] bc [d]', '[', ']');

// 'a'

Str::camel()

Str::camel 方法将给定字符串转换为 camelCase(驼峰命名法):

php
use Illuminate\Support\Str;

$converted = Str::camel('foo_bar');

// 'fooBar'

Str::charAt()

Str::charAt 方法返回指定索引处的字符。如果索引超出范围,则返回 false

php
use Illuminate\Support\Str;

$character = Str::charAt('This is my name.', 6);

// 's'

Str::chopStart()

Str::chopStart 方法仅当值出现在字符串开头时,才删除该值的第一次出现:

php
use Illuminate\Support\Str;

$url = Str::chopStart('https://laravel.com', 'https://');

// 'laravel.com'

您也可以传递数组作为第二个参数。如果字符串以数组中的任何值开头,则该值将从字符串中删除:

php
use Illuminate\Support\Str;

$url = Str::chopStart('http://laravel.com', ['https://', 'http://']);

// 'laravel.com'

Str::chopEnd()

Str::chopEnd 方法仅当值出现在字符串末尾时,才删除该值的最后一次出现:

php
use Illuminate\Support\Str;

$url = Str::chopEnd('app/Models/Photograph.php', '.php');

// 'app/Models/Photograph'

您也可以传递数组作为第二个参数。如果字符串以数组中的任何值结尾,则该值将从字符串中删除:

php
use Illuminate\Support\Str;

$url = Str::chopEnd('laravel.com/index.php', ['/index.html', '/index.php']);

// 'laravel.com'

Str::contains()

Str::contains 方法确定给定字符串是否包含给定值。默认情况下,此方法区分大小写:

php
use Illuminate\Support\Str;

$contains = Str::contains('This is my name', 'my');

// true

您也可以传递值数组来确定给定字符串是否包含数组中的任何值:

php
use Illuminate\Support\Str;

$contains = Str::contains('This is my name', ['my', 'foo']);

// true

您可以通过将 ignoreCase 参数设置为 true 来禁用大小写敏感:

php
use Illuminate\Support\Str;

$contains = Str::contains('This is my name', 'MY', ignoreCase: true);

// true

Str::containsAll()

Str::containsAll 方法确定给定字符串是否包含给定数组中的所有值:

php
use Illuminate\Support\Str;

$containsAll = Str::containsAll('This is my name', ['my', 'name']);

// true

您可以通过将 ignoreCase 参数设置为 true 来禁用大小写敏感:

php
use Illuminate\Support\Str;

$containsAll = Str::containsAll('This is my name', ['MY', 'NAME'], ignoreCase: true);

// true

Str::doesntContain()

Str::doesntContain 方法确定给定字符串是否不包含给定值。默认情况下,此方法区分大小写:

php
use Illuminate\Support\Str;

$doesntContain = Str::doesntContain('This is name', 'my');

// true

您也可以传递值数组来确定给定字符串是否不包含数组中的任何值:

php
use Illuminate\Support\Str;

$doesntContain = Str::doesntContain('This is name', ['my', 'framework']);

// true

您可以通过将 ignoreCase 参数设置为 true 来禁用大小写敏感:

php
use Illuminate\Support\Str;

$doesntContain = Str::doesntContain('This is name', 'MY', ignoreCase: true);

// true

Str::deduplicate()

Str::deduplicate 方法将给定字符串中字符的连续实例替换为该字符的单个实例。默认情况下,该方法去除重复的空格:

php
use Illuminate\Support\Str;

$result = Str::deduplicate('The   Laravel   Framework');

// The Laravel Framework

您可以通过将字符作为第二个参数传递给方法来指定要去除重复的不同字符:

php
use Illuminate\Support\Str;

$result = Str::deduplicate('The---Laravel---Framework', '-');

// The-Laravel-Framework

Str::doesntEndWith()

Str::doesntEndWith 方法确定给定字符串是否不以给定值结尾:

php
use Illuminate\Support\Str;

$result = Str::doesntEndWith('This is my name', 'dog');

// true

您也可以传递值数组来确定给定字符串是否不以数组中的任何值结尾:

php
use Illuminate\Support\Str;

$result = Str::doesntEndWith('This is my name', ['this', 'foo']);

// true

$result = Str::doesntEndWith('This is my name', ['name', 'foo']);

// false

Str::doesntStartWith()

Str::doesntStartWith 方法确定给定字符串是否不以给定值开头:

php
use Illuminate\Support\Str;

$result = Str::doesntStartWith('This is my name', 'That');

// true

如果传递可能值的数组,如果字符串不以任何给定值开头,doesntStartWith 方法将返回 true

php
$result = Str::doesntStartWith('This is my name', ['What', 'That', 'There']);

// true

Str::endsWith()

Str::endsWith 方法确定给定字符串是否以给定值结尾:

php
use Illuminate\Support\Str;

$result = Str::endsWith('This is my name', 'name');

// true

您也可以传递值数组来确定给定字符串是否以数组中的任何值结尾:

php
use Illuminate\Support\Str;

$result = Str::endsWith('This is my name', ['name', 'foo']);

// true

$result = Str::endsWith('This is my name', ['this', 'foo']);

// false

Str::excerpt()

Str::excerpt 方法从给定字符串中提取与该字符串中短语的第一个实例匹配的摘录:

php
use Illuminate\Support\Str;

$excerpt = Str::excerpt('This is my name', 'my', [
    'radius' => 3
]);

// '...is my na...'

radius 选项默认为 100,允许您定义截断字符串每侧应出现的字符数。

此外,您可以使用 omission 选项定义将附加到截断字符串开头和结尾的字符串:

php
use Illuminate\Support\Str;

$excerpt = Str::excerpt('This is my name', 'name', [
    'radius' => 3,
    'omission' => '(...) '
]);

// '(...) my name'

Str::finish()

Str::finish 方法如果字符串尚未以给定值结尾,则向字符串添加该值的单个实例:

php
use Illuminate\Support\Str;

$adjusted = Str::finish('this/string', '/');

// this/string/

$adjusted = Str::finish('this/string/', '/');

// this/string/

Str::fromBase64()