字符串
简介
Laravel 包含多种操作字符串值的函数。其中许多函数由框架本身使用;但是,如果您觉得方便,可以在自己的应用程序中自由使用它们。
可用方法
字符串
__class_basenameepreg_replace_arrayStr::afterStr::afterLastStr::apaStr::asciiStr::beforeStr::beforeLastStr::betweenStr::betweenFirstStr::camelStr::charAtStr::chopStartStr::chopEndStr::containsStr::containsAllStr::doesntContainStr::doesntEndWithStr::doesntStartWithStr::deduplicateStr::endsWithStr::excerptStr::finishStr::fromBase64Str::headlineStr::initialsStr::inlineMarkdownStr::isStr::isAsciiStr::isJsonStr::isUlidStr::isUrlStr::isUuidStr::kebabStr::lcfirstStr::lengthStr::limitStr::lowerStr::markdownStr::maskStr::matchStr::matchAllStr::isMatchStr::orderedUuidStr::padBothStr::padLeftStr::padRightStr::passwordStr::pluralStr::pluralStudlyStr::positionStr::randomStr::removeStr::repeatStr::replaceStr::replaceArrayStr::replaceFirstStr::replaceLastStr::replaceMatchesStr::replaceStartStr::replaceEndStr::reverseStr::singularStr::slugStr::snakeStr::squishStr::startStr::startsWithStr::studlyStr::substrStr::substrCountStr::substrReplaceStr::swapStr::takeStr::titleStr::toBase64Str::transliterateStr::trimStr::ltrimStr::rtrimStr::ucfirstStr::ucsplitStr::ucwordsStr::upperStr::ulidStr::unwrapStr::uuidStr::uuid7Str::wordCountStr::wordWrapStr::wordsStr::wrapstrtranstrans_choice
流畅字符串
afterafterLastapaappendasciibasenamebeforebeforeLastbetweenbetweenFirstcamelcharAtclassBasenamechopStartchopEndcontainscontainsAlldecryptdeduplicatedirnamedoesntContaindoesntEndWithdoesntStartWithencryptendsWithexactlyexcerptexplodefinishfromBase64hashheadlineinitialsinlineMarkdownisisAsciiisEmptyisNotEmptyisJsonisUlidisUrlisUuidkebablcfirstlengthlimitlowermarkdownmaskmatchmatchAllisMatchnewLinepadBothpadLeftpadRightpipepluralpositionprependremoverepeatreplacereplaceArrayreplaceFirstreplaceLastreplaceMatchesreplaceStartreplaceEndscansingularslugsnakesplitsquishstartstartsWithstripTagsstudlysubstrsubstrReplaceswaptaketaptesttitletoBase64toHtmlStringtoUritransliteratetrimltrimrtrimucfirstucsplitucwordsunwrapupperwhenwhenContainswhenContainsAllwhenDoesntEndWithwhenDoesntStartWithwhenEmptywhenNotEmptywhenStartsWithwhenEndsWithwhenExactlywhenNotExactlywhenIswhenIsAsciiwhenIsUlidwhenIsUuidwhenTestwordCountwordswrap
字符串
__()
__ 函数使用您的语言文件翻译给定的翻译字符串或翻译键:
echo __('Welcome to our application');
echo __('messages.welcome');如果指定的翻译字符串或键不存在,__ 函数将返回给定的值。因此,使用上面的示例,如果该翻译键不存在,__ 函数将返回 messages.welcome。
class_basename()
class_basename 函数返回给定类的类名,并去除类的命名空间:
$class = class_basename('Foo\Bar\Baz');
// Baze()
e 函数运行 PHP 的 htmlspecialchars 函数,默认将 double_encode 选项设置为 true:
echo e('<html>foo</html>');
// <html>foo</html>preg_replace_array()
preg_replace_array 函数使用数组按顺序替换字符串中的给定模式:
$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:00Str::after()
Str::after 方法返回字符串中给定值之后的所有内容。如果该值在字符串中不存在,则返回整个字符串:
use Illuminate\Support\Str;
$slice = Str::after('This is my name', 'This is');
// ' my name'Str::afterLast()
Str::afterLast 方法返回字符串中给定值最后一次出现之后的所有内容。如果该值在字符串中不存在,则返回整个字符串:
use Illuminate\Support\Str;
$slice = Str::afterLast('App\Http\Controllers\Controller', '\\');
// 'Controller'Str::apa()
Str::apa 方法按照 APA 指南将给定字符串转换为标题大小写:
use Illuminate\Support\Str;
$title = Str::apa('Creating A Project');
// 'Creating a Project'Str::ascii()
Str::ascii 方法将尝试将字符串音译为 ASCII 值:
use Illuminate\Support\Str;
$slice = Str::ascii('û');
// 'u'Str::before()
Str::before 方法返回字符串中给定值之前的所有内容:
use Illuminate\Support\Str;
$slice = Str::before('This is my name', 'my name');
// 'This is 'Str::beforeLast()
Str::beforeLast 方法返回字符串中给定值最后一次出现之前的所有内容:
use Illuminate\Support\Str;
$slice = Str::beforeLast('This is my name', 'is');
// 'This 'Str::between()
Str::between 方法返回字符串中两个值之间的部分:
use Illuminate\Support\Str;
$slice = Str::between('This is my name', 'This', 'name');
// ' is my 'Str::betweenFirst()
Str::betweenFirst 方法返回字符串中两个值之间的最小可能部分:
use Illuminate\Support\Str;
$slice = Str::betweenFirst('[a] bc [d]', '[', ']');
// 'a'Str::camel()
Str::camel 方法将给定字符串转换为 camelCase(驼峰命名法):
use Illuminate\Support\Str;
$converted = Str::camel('foo_bar');
// 'fooBar'Str::charAt()
Str::charAt 方法返回指定索引处的字符。如果索引超出范围,则返回 false:
use Illuminate\Support\Str;
$character = Str::charAt('This is my name.', 6);
// 's'Str::chopStart()
Str::chopStart 方法仅当值出现在字符串开头时,才删除该值的第一次出现:
use Illuminate\Support\Str;
$url = Str::chopStart('https://laravel.com', 'https://');
// 'laravel.com'您也可以传递数组作为第二个参数。如果字符串以数组中的任何值开头,则该值将从字符串中删除:
use Illuminate\Support\Str;
$url = Str::chopStart('http://laravel.com', ['https://', 'http://']);
// 'laravel.com'Str::chopEnd()
Str::chopEnd 方法仅当值出现在字符串末尾时,才删除该值的最后一次出现:
use Illuminate\Support\Str;
$url = Str::chopEnd('app/Models/Photograph.php', '.php');
// 'app/Models/Photograph'您也可以传递数组作为第二个参数。如果字符串以数组中的任何值结尾,则该值将从字符串中删除:
use Illuminate\Support\Str;
$url = Str::chopEnd('laravel.com/index.php', ['/index.html', '/index.php']);
// 'laravel.com'Str::contains()
Str::contains 方法确定给定字符串是否包含给定值。默认情况下,此方法区分大小写:
use Illuminate\Support\Str;
$contains = Str::contains('This is my name', 'my');
// true您也可以传递值数组来确定给定字符串是否包含数组中的任何值:
use Illuminate\Support\Str;
$contains = Str::contains('This is my name', ['my', 'foo']);
// true您可以通过将 ignoreCase 参数设置为 true 来禁用大小写敏感:
use Illuminate\Support\Str;
$contains = Str::contains('This is my name', 'MY', ignoreCase: true);
// trueStr::containsAll()
Str::containsAll 方法确定给定字符串是否包含给定数组中的所有值:
use Illuminate\Support\Str;
$containsAll = Str::containsAll('This is my name', ['my', 'name']);
// true您可以通过将 ignoreCase 参数设置为 true 来禁用大小写敏感:
use Illuminate\Support\Str;
$containsAll = Str::containsAll('This is my name', ['MY', 'NAME'], ignoreCase: true);
// trueStr::doesntContain()
Str::doesntContain 方法确定给定字符串是否不包含给定值。默认情况下,此方法区分大小写:
use Illuminate\Support\Str;
$doesntContain = Str::doesntContain('This is name', 'my');
// true您也可以传递值数组来确定给定字符串是否不包含数组中的任何值:
use Illuminate\Support\Str;
$doesntContain = Str::doesntContain('This is name', ['my', 'framework']);
// true您可以通过将 ignoreCase 参数设置为 true 来禁用大小写敏感:
use Illuminate\Support\Str;
$doesntContain = Str::doesntContain('This is name', 'MY', ignoreCase: true);
// trueStr::deduplicate()
Str::deduplicate 方法将给定字符串中字符的连续实例替换为该字符的单个实例。默认情况下,该方法去除重复的空格:
use Illuminate\Support\Str;
$result = Str::deduplicate('The Laravel Framework');
// The Laravel Framework您可以通过将字符作为第二个参数传递给方法来指定要去除重复的不同字符:
use Illuminate\Support\Str;
$result = Str::deduplicate('The---Laravel---Framework', '-');
// The-Laravel-FrameworkStr::doesntEndWith()
Str::doesntEndWith 方法确定给定字符串是否不以给定值结尾:
use Illuminate\Support\Str;
$result = Str::doesntEndWith('This is my name', 'dog');
// true您也可以传递值数组来确定给定字符串是否不以数组中的任何值结尾:
use Illuminate\Support\Str;
$result = Str::doesntEndWith('This is my name', ['this', 'foo']);
// true
$result = Str::doesntEndWith('This is my name', ['name', 'foo']);
// falseStr::doesntStartWith()
Str::doesntStartWith 方法确定给定字符串是否不以给定值开头:
use Illuminate\Support\Str;
$result = Str::doesntStartWith('This is my name', 'That');
// true如果传递可能值的数组,如果字符串不以任何给定值开头,doesntStartWith 方法将返回 true:
$result = Str::doesntStartWith('This is my name', ['What', 'That', 'There']);
// trueStr::endsWith()
Str::endsWith 方法确定给定字符串是否以给定值结尾:
use Illuminate\Support\Str;
$result = Str::endsWith('This is my name', 'name');
// true您也可以传递值数组来确定给定字符串是否以数组中的任何值结尾:
use Illuminate\Support\Str;
$result = Str::endsWith('This is my name', ['name', 'foo']);
// true
$result = Str::endsWith('This is my name', ['this', 'foo']);
// falseStr::excerpt()
Str::excerpt 方法从给定字符串中提取与该字符串中短语的第一个实例匹配的摘录:
use Illuminate\Support\Str;
$excerpt = Str::excerpt('This is my name', 'my', [
'radius' => 3
]);
// '...is my na...'radius 选项默认为 100,允许您定义截断字符串每侧应出现的字符数。
此外,您可以使用 omission 选项定义将附加到截断字符串开头和结尾的字符串:
use Illuminate\Support\Str;
$excerpt = Str::excerpt('This is my name', 'name', [
'radius' => 3,
'omission' => '(...) '
]);
// '(...) my name'Str::finish()
Str::finish 方法如果字符串尚未以给定值结尾,则向字符串添加该值的单个实例:
use Illuminate\Support\Str;
$adjusted = Str::finish('this/string', '/');
// this/string/
$adjusted = Str::finish('this/string/', '/');
// this/string/