字串
📝 此頁面為 Laravel 官方文檔的繁體中文翻譯。查看原始英文版本
字串
簡介
Laravel 包含各種用於操作字串值的函數。這些函數中有許多被框架本身使用;但是,如果您發現它們很方便,您可以自由地在自己的應用程式中使用它們。
可用方法
字串
__ class_basename e preg_replace_array Str::after Str::afterLast Str::apa Str::ascii Str::before Str::beforeLast Str::between Str::betweenFirst Str::camel Str::charAt Str::chopStart Str::chopEnd Str::contains Str::containsAll Str::doesntContain Str::doesntEndWith Str::doesntStartWith Str::deduplicate Str::endsWith Str::excerpt Str::finish Str::fromBase64 Str::headline Str::initials Str::inlineMarkdown Str::is Str::isAscii Str::isJson Str::isUlid Str::isUrl Str::isUuid Str::kebab Str::lcfirst Str::length Str::limit Str::lower Str::markdown Str::mask Str::match Str::matchAll Str::isMatch Str::orderedUuid Str::padBoth Str::padLeft Str::padRight Str::password Str::plural Str::pluralStudly Str::position Str::random Str::remove Str::repeat Str::replace Str::replaceArray Str::replaceFirst Str::replaceLast Str::replaceMatches Str::replaceStart Str::replaceEnd Str::reverse Str::singular Str::slug Str::snake Str::squish Str::start Str::startsWith Str::studly Str::substr Str::substrCount Str::substrReplace Str::swap Str::take Str::title Str::toBase64 Str::transliterate Str::trim Str::ltrim Str::rtrim Str::ucfirst Str::ucsplit Str::ucwords Str::upper Str::ulid Str::unwrap Str::uuid Str::uuid7 Str::wordCount Str::wordWrap Str::words Str::wrap str trans trans_choice
流暢字串
after afterLast apa append ascii basename before beforeLast between betweenFirst camel charAt classBasename chopStart chopEnd contains containsAll decrypt deduplicate dirname doesntContain doesntEndWith doesntStartWith encrypt endsWith exactly excerpt explode finish fromBase64 hash headline initials inlineMarkdown is isAscii isEmpty isNotEmpty isJson isUlid isUrl isUuid kebab lcfirst length limit lower markdown mask match matchAll isMatch newLine padBoth padLeft padRight pipe plural position prepend remove repeat replace replaceArray replaceFirst replaceLast replaceMatches replaceStart replaceEnd scan singular slug snake split squish start startsWith stripTags studly substr substrReplace swap take tap test title toBase64 toHtmlString toUri transliterate trim ltrim rtrim ucfirst ucsplit ucwords unwrap upper when whenContains whenContainsAll whenDoesntEndWith whenDoesntStartWith whenEmpty whenNotEmpty whenStartsWith whenEndsWith whenExactly whenNotExactly whenIs whenIsAscii whenIsUlid whenIsUuid whenTest wordCount words wrap
字串
__() {.collection-method}
__ 函數使用您的語言檔案翻譯給定的翻譯字串或翻譯金鑰:
echo __('Welcome to our application');
echo __('messages.welcome');
如果指定的翻譯字串或金鑰不存在,__ 函數將返回給定的值。因此,使用上面的範例,如果該翻譯金鑰不存在,__ 函數將返回 messages.welcome。
class_basename() {.collection-method}
class_basename 函數返回給定類別的類別名稱,不包含類別的命名空間:
$class = class_basename('Foo\Bar\Baz');
// Baz
e() {.collection-method}
e 函數運行 PHP 的 htmlspecialchars 函數,double_encode 選項預設設為 true:
echo e('<html>foo</html>');
// <html>foo</html>
preg_replace_array() {.collection-method}
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:00
Str::after() {.collection-method}
Str::after 方法返回字串中給定值之後的所有內容:
use Illuminate\Support\Str;
$slice = Str::after('This is my name', 'This is');
// ' my name'
Str::afterLast() {.collection-method}
Str::afterLast 方法返回字串中最後一次出現的給定值之後的所有內容:
use Illuminate\Support\Str;
$slice = Str::afterLast('App\Http\Controllers\Controller', '\\');
// 'Controller'
Str::apa() {.collection-method}
Str::apa 方法根據 APA 指南將給定的字串轉換為標題大小寫:
use Illuminate\Support\Str;
$title = Str::apa('Creating A Project');
// 'Creating a Project'
Str::ascii() {.collection-method}
Str::ascii 方法將嘗試將字串轉寫為 ASCII 值:
use Illuminate\Support\Str;
$slice = Str::ascii('û');
// 'u'
Str::before() {.collection-method}
Str::before 方法返回字串中給定值之前的所有內容:
use Illuminate\Support\Str;
$slice = Str::before('This is my name', 'my name');
// 'This is '
Str::beforeLast() {.collection-method}
Str::beforeLast 方法返回字串中最後一次出現的給定值之前的所有內容:
use Illuminate\Support\Str;
$slice = Str::beforeLast('This is my name', 'is');
// 'This '
Str::between() {.collection-method}
Str::between 方法返回字串中兩個值之間的部分:
use Illuminate\Support\Str;
$slice = Str::between('This is my name', 'This', 'name');
// ' is my '
Str::betweenFirst() {.collection-method}
Str::betweenFirst 方法返回字串中兩個值之間最小可能的部分:
use Illuminate\Support\Str;
$slice = Str::betweenFirst('[a] bc [d]', '[', ']');
// 'a'
Str::camel() {.collection-method}
Str::camel 方法將給定的字串轉換為 camelCase:
use Illuminate\Support\Str;
$converted = Str::camel('foo_bar');
// 'fooBar'
Str::charAt() {.collection-method}
Str::charAt 方法返回指定索引處的字元。如果索引超出範圍,則返回 false:
use Illuminate\Support\Str;
$character = Str::charAt('This is my name.', 6);
// 's'
Str::chopStart() {.collection-method}
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() {.collection-method}
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() {.collection-method}
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);
// true
Str::containsAll() {.collection-method}
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);
// true
Str::doesntContain() {.collection-method}
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);
// true
Str::deduplicate() {.collection-method}
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-Framework
Str::doesntEndWith() {.collection-method}
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']);
// false
Str::doesntStartWith() {.collection-method}
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']);
// true
Str::endsWith() {.collection-method}
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']);
// false
Str::excerpt() {.collection-method}
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() {.collection-method}
Str::finish 方法在字串不以給定的值結尾時,向字串添加給定值的單個實例:
use Illuminate\Support\Str;
$adjusted = Str::finish('this/string', '/');
// this/string/
$adjusted = Str::finish('this/string/', '/');
// this/string/
Str::fromBase64() {.collection-method}
Str::fromBase64 方法解碼給定的 Base64 字串:
use Illuminate\Support\Str;
$decoded = Str::fromBase64('TGFyYXZlbA==');
// Laravel
Str::headline() {.collection-method}
Str::headline 方法將由大小寫、連字號或下劃線分隔的字串轉換為以空格分隔的字串,每個單詞的首字母大寫:
use Illuminate\Support\Str;
$headline = Str::headline('steve_jobs');
// Steve Jobs
$headline = Str::headline('EmailNotificationSent');
// Email Notification Sent
Str::initials() {.collection-method}
Str::initials 方法將返回給定字串的首字母縮寫,可選擇將其大寫:
use Illuminate\Support\Str;
$initials = Str::initials('taylor otwell');
// to
$initials = Str::initials('taylor otwell', capitalize: true);
// TO
Str::inlineMarkdown() {.collection-method}
Str::inlineMarkdown 方法將給定的字串轉換為 HTML,使用聯盟通用標記,但保留內聯 Markdown:
use Illuminate\Support\Str;
$html = Str::inlineMarkdown('**Laravel**');
// <strong>Laravel</strong>
Str::is() {.collection-method}
Str::is 方法判斷給定的字串是否與給定的模式匹配。星號 * 可用作萬用字元:
use Illuminate\Support\Str;
$matches = Str::is('foo*', 'foobar'); // true
$matches = Str::is('bar*', 'foobar'); // false
Str::isAscii() {.collection-method}
Str::isAscii 方法判斷給定的字串是否包含 ASCII 字元:
use Illuminate\Support\Str;
$isAscii = Str::isAscii('Taylor');
// true
$isAscii = Str::isAscii(''''');
// false
Str::isJson() {.collection-method}
Str::isJson 方法判斷給定的字串是否是有效的 JSON:
use Illuminate\Support\Str;
$result = Str::isJson('[1,2,3]');
// true
$result = Str::isJson('{"name": "Taylor"}');
// true
$result = Str::isJson('{name: "Taylor"}');
// false
Str::isUlid() {.collection-method}
Str::isUlid 方法判斷給定的字串是否是有效的 ULID:
use Illuminate\Support\Str;
$result = Str::isUlid('01gd4d3tgrrfqeda94gdbtdk5c');
// true
$result = Str::isUlid('laravel');
// false
Str::isUrl() {.collection-method}
Str::isUrl 方法判斷給定的字串是否是有效的 URL:
use Illuminate\Support\Str;
$result = Str::isUrl('https://laravel.com');
// true
$result = Str::isUrl('laravel.com');
// true
$result = Str::isUrl('not a url');
// false
Str::isUuid() {.collection-method}
Str::isUuid 方法判斷給定的字串是否是有效的 UUID:
use Illuminate\Support\Str;
$result = Str::isUuid('5b9e3467-2c4f-4d5a-b9b5-4f6f7f8f9d0a');
// true
$result = Str::isUuid('laravel');
// false
Str::kebab() {.collection-method}
Str::kebab 方法將給定的字串轉換為 kebab-case:
use Illuminate\Support\Str;
$converted = Str::kebab('fooBar');
// foo-bar
Str::lcfirst() {.collection-method}
Str::lcfirst 方法將給定字串的第一個字元轉換為小寫:
use Illuminate\Support\Str;
$converted = Str::lcfirst('Hello World');
// hello World
Str::length() {.collection-method}
Str::length 方法返回給定字串的長度:
use Illuminate\Support\Str;
$length = Str::length('Laravel');
// 6
Str::limit() {.collection-method}
Str::limit 方法將字串截斷為給定的最大長度。此方法將在字串的末尾附加 ...:
use Illuminate\Support\Str;
$result = Str::limit('The quick brown fox jumps over the lazy dog.', 20);
// "The quick brown fox..."
您還可以將第三個參數傳遞給 limit 方法以更改將附加到截斷字串末尾的字串:
use Illuminate\Support\Str;
$result = Str::limit('The quick brown fox jumps over the lazy dog.', 20, '>>>');
// "The quick brown fox>>>"
Str::lower() {.collection-method}
Str::lower 方法將給定的字串轉換為小寫:
use Illuminate\Support\Str;
$converted = Str::lower('LARAVEL');
// "laravel"
Str::markdown() {.collection-method}
Str::markdown 方法將給定的字串轉換為 HTML,使用聯盟通用標記:
use Illuminate\Support\Str;
$html = Str::markdown('# Laravel');
// <h1>Laravel</h1>
Str::mask() {.collection-method}
Str::mask 方法遮罩字串的一部分,從給定長度的第二個位置開始:
use Illuminate\Support\Str;
$masked = Str::mask('[email protected]', '*', 3);
// "***[email protected]"
如果需要,您可以為 mask 方法提供第三個參數來改變遮罩的長度:
use Illuminate\Support\Str;
$masked = Str::mask('[email protected]', '*', -8);
// "taylor@***"
如果需要,您可以為 mask 方法提供第四個參數來更改要替換的字元:
use Illuminate\Support\Str;
$masked = Str::mask('[email protected]', '-', 3);
// "[email protected]"
Str::match() {.collection-method}
Str::match 方法返回給定正規表達式的匹配:
use Illuminate\Support\Str;
$match = Str::match('foo bar', '/bar/');
// "bar"
Str::matchAll() {.collection-method}
Str::matchAll 方法返回一個包含給定正規表達式所有匹配的陣列:
use Illuminate\Support\Str;
$matches = Str::matchAll('bar foo bar', '/bar/');
// [0 => ['bar', 'bar']]
Str::isMatch() {.collection-method}
Str::isMatch 方法判斷給定的字串是否匹配給定的正規表達式:
use Illuminate\Support\Str;
$result = Str::isMatch('laravel', '/^lar/');
// true
Str::orderedUuid() {.collection-method}
orderedUuid 方法生成一個可排序的 UUIDv7 實例。此 UUID 對於索引資料庫儲存非常有效,因為它們可以按字典順序排序:
use Illuminate\Support\Str;
(Str::orderedUuid())->toString(); // "01925262-c6ca-7c8a-a00c-078378149ad9"
Str::padBoth() {.collection-method}
Str::padBoth 方法使用給定的填充字串在字串的兩側填充,使其達到給定的長度:
use Illuminate\Support\Str;
$padded = Str::padBoth('Taylor', 10, '_');
// "__Taylor__"
Str::padLeft() {.collection-method}
Str::padLeft 方法使用給定的填充字串在字串的左側填充,使其達到給定的長度:
use Illuminate\Support\Str;
$padded = Str::padLeft('Taylor', 10, '0');
// "0000Taylor"
Str::padRight() {.collection-method}
Str::padRight 方法使用給定的填充字串在字串的右側填充,使其達到給定的長度:
use Illuminate\Support\Str;
$padded = Str::padRight('Taylor', 10, '_');
// "Taylor____"
Str::password() {.collection-method}
Str::password 方法生成一個安全的隨機密碼:
use Illuminate\Support\Str;
$password = Str::password();
// "fOcikIcv8C8rO3BKHlHJ"
$password = Str::password(40);
// "uSan1HinzcCiPcTuwOx3TapxNd7dAfhKf5Ew1Tlg"
$password = Str::password(40, true);
// "1eNYIWrq3A5rV2fDpEH1YQWnH9xQO0Kl"
Str::plural() {.collection-method}
Str::plural 方法將字串從單數轉換為複數。Laravel 的 pluralizer 基於 ramsey/inflect 包,支援多種語言:
use Illuminate\Support\Str;
$plural = Str::plural('car');
// cars
$plural = Str::plural('child');
// children
您可以將第二個參數傳遞給 plural 方法以指定複數形式的數量:
use Illuminate\Support\Str;
$plural = Str::plural('apple', 0);
// apples
$plural = Str::plural('apple', 1);
// apple
$plural = Str::plural('apple', 2);
// apples
Str::pluralStudly() {.collection-method}
Str::pluralStudly 方法使用大寫蛇形命名法將字串從單數轉換為複數:
use Illuminate\Support\Str;
$plural = Str::pluralStudly('has_time');
// has_times
$plural = Str::pluralStudly('has_thought');
// has_thoughts
Str::position() {.collection-method}
Str::position 方法返回子字串在給定字串中的位置:
use Illuminate\Support\Str;
$position = Str::position('Hello, World!', 'World');
// 7
Str::random() {.collection-method}
Str::random 方法生成一個給定長度的隨機字串:
use Illuminate\Support\Str;
$random = Str::random(40);
// "vAnT8S8FVWd9z6Z8Xf8u3b1s9a2c4e5f6g7h8"
Str::remove() {.collection-method}
Str::remove 方法從字串中移除一個子字串:
use Illuminate\Support\Str;
$string = Str::remove('Peter Piper picked a peck of pickled peppers.', ['pecker', 'pickled', 'piper']);
// "Peter Piper picked a peck of peppers."
Str::repeat() {.collection-method}
Str::repeat 方法重複給定的字串給定的次數:
use Illuminate\Support\Str;
$repeated = Str::repeat('abc', 3);
// "abcabcabc"
Str::replace() {.collection-method}
Str::replace 方法替換給定的字串中的一個子字串:
use Illuminate\Support\Str;
$string = Str::replace('foo is the best?', 'bar', 'best');
// "foo is the bar?"
Str::replaceArray() {.collection-method}
Str::replaceArray 方法使用陣列中的值按順序替換給定字串中的佔位符:
use Illuminate\Support\Str;
$string = 'The event will take place on :first and :second';
$formatted = Str::replaceArray($string, [':first', ':second'], ['November', 'December']);
// "The event will take place on November and December"
Str::replaceFirst() {.collection-method}
Str::replaceFirst 方法替換給定字串中子字串的第一次出現:
use Illuminate\Support\Str;
$string = 'The quick brown fox jumps over the lazy dog.';
$formatted = Str::replaceFirst($string, 'the', 'a');
// "A quick brown fox jumps over the lazy dog."
Str::replaceLast() {.collection-method}
Str::replaceLast 方法替換給定字串中子字串的最後一次出現:
use Illuminate\Support\Str;
$string = 'The quick brown fox jumps over the lazy dog.';
$formatted = Str::replaceLast($string, 'the', 'a');
// "The quick brown fox jumps over a lazy dog."
Str::replaceMatches() {.collection-method}
Str::replaceMatches 方法替換給定字串中正規表達式的匹配:
use Illuminate\Support\Str;
$result = Str::replaceMatches('/[a-z]/', function (array $matches) {
return strtoupper($matches[0]);
}, 'Taylor the Foobar');
// "TAYLOR THE FOOBAR"
Str::replaceStart() {.collection-method}
Str::replaceStart 方法僅在字串以給定值開頭時替換其第一次出現:
use Illuminate\Support\Str;
$replaced = Str::replaceStart('Hello World', 'Hello', 'Greetings');
// "Greetings World"
Str::replaceEnd() {.collection-method}
Str::replaceEnd 方法僅在字串以給定值結尾時替換其最後一次出現:
use Illuminate\Support\Str;
$replaced = Str::replaceEnd('Hello World', 'World', 'Laravel');
// "Hello Laravel"
Str::reverse() {.collection-method}
Str::reverse 方法反轉給定的字串:
use Illuminate\Support\Str;
$reversed = Str::reverse('Hello World');
// dlroW olleH
Str::singular() {.collection-method}
Str::singular 方法將字串從複數轉換為單數:
use Illuminate\Support\Str;
$singular = Str::singular('cars');
// car
$singular = Str::singular('children');
// child
Str::slug() {.collection-method}
Str::slug 方法生成一個適合放在 URL 中的給定字串的簡潔表示:
use Illuminate\Support\Str;
$slug = Str::slug('Laravel 5.7 Framework');
// "laravel-5.7-framework"
Str::snake() {.collection-method}
Str::snake 方法將給定的字串轉換為 snake_case:
use Illuminate\Support\Str;
$converted = Str::snake('FooBar');
// foo_bar
Str::squish() {.collection-method}
Str::squish 方法壓縮給定字串中的所有空白:
use Illuminate\Support\Str;
$squished = Str::squish(' laravel framework ');
// "laravel framework"
Str::start() {.collection-method}
Str::start 方法在字串的開頭添加給定的值,如果它尚未以給定的值開頭:
use Illuminate\Support\Str;
$adjusted = Str::start($string, 'this-is-a-');
$adjusted = Str::start('this-is-a-string', 'this-is-a-');
// this-is-a-string
$adjusted = Str::start('awesome-string', 'this-is-a-');
// this-is-a-awesome-string
Str::startsWith() {.collection-method}
Str::startsWith 方法判斷給定的字串是否以另一個子字串開頭:
use Illuminate\Support\Str;
$result = Str::startsWith('This is my name', 'This');
// true
$result = Str::startsWith('This is my name', ['This', 'That']);
// true
$result = Str::startsWith('This is my name', ['that', 'there']);
// false
Str::studly() {.collection-method}
Str::studly 方法將給定的字串轉換為大寫蛇形命名法:
use Illuminate\Support\Str;
$converted = Str::studly('foo_bar');
// FooBar
Str::substr() {.collection-method}
Str::substr 方法返回字串的給定部分:
use Illuminate\Support\Str;
$string = 'Laravel 13';
$sub = Str::substr($string, 0, 6);
// "Laravel"
Str::substrCount() {.collection-method}
Str::substrCount 方法返回給定的子字串在字串中出現的次數:
use Illuminate\Support\Str;
$count = Str::substrCount('This is a test, test.', 'test');
// 2
Str::substrReplace() {.collection-method}
Str::substrReplace 方法替換字串的給定部分:
use Illuminate\Support\Str;
$result = Str::substrReplace('The Laravel Framework', 'Framework', 10);
// The Laravel Framework
$result = Str::substrReplace('The Laravel Framework', 'Framework', 10, 3);
// The Laravel Frwork
Str::swap() {.collection-method}
Str::swap 方法使用陣列中提供的鍵值對替換字串中的多個值:
use Illuminate\Support\Str;
$string = Str::swap([
'Tacos' => 'Burritos',
'great' => 'fantastic',
], 'Tacos are great!');
// "Burritos are fantastic!"
Str::take() {.collection-method}
Str::take 方法返回字串的給定部分:
use Illuminate\Support\Str;
$taken = Str::take('The quick brown fox jumps over the lazy dog.', 5);
// "The q"
Str::title() {.collection-method}
Str::title 方法將給定的字串轉換為標題大小寫:
use Illuminate\Support\Str;
$converted = Str::title('a nice title uses the correct case');
// "A Nice Title Uses The Correct Case"
Str::toBase64() {.collection-method}
Str::toBase64 方法將給定的字串編碼為 Base64:
use Illuminate\Support\Str;
$encoded = Str::toBase64('Laravel');
// "TGFyYXZlbA=="
Str::transliterate() {.collection-method}
Str::transliterate 方法將字串轉寫為與 ASCII 相似的字元:
use Illuminate\Support\Str;
$transliterated = Str::transliterate('Über');
// Uber
Str::trim() {.collection-method}
Str::trim 方法修剪字串兩端的空白字元和其他字元:
use Illuminate\Support\Str;
$trimmed = Str::trim(' Laravel ');
// "Laravel"
Str::ltrim() {.collection-method}
Str::ltrim 方法修剪字串左側的空白字元和其他字元:
use Illuminate\Support\Str;
$trimmed = Str::ltrim(' Laravel ');
// "Laravel "
Str::rtrim() {.collection-method}
Str::rtrim 方法修剪字串右側的空白字元和其他字元:
use Illuminate\Support\Str;
$trimmed = Str::rtrim(' Laravel ');
// " Laravel"
Str::ucfirst() {.collection-method}
Str::ucfirst 方法將給定字串的第一個字元轉換為大寫:
use Illuminate\Support\Str;
$converted = Str::ucfirst('hello world');
// "Hello world"
Str::ucsplit() {.collection-method}
Str::ucsplit 方法將給定的字串按大寫字元拆分為陣列:
use Illuminate\Support\Str;
$splits = Str::ucsplit('fooBar');
// [0 => 'foo', 1 => 'Bar']
Str::ucwords() {.collection-method}
Str::ucwords 方法將給定字串中每個單詞的首字母轉換為大寫:
use Illuminate\Support\Str;
$converted = Str::ucwords('hello world');
// "Hello World"
Str::upper() {.collection-method}
Str::upper 方法將給定的字串轉換為大寫:
use Illuminate\Support\Str;
$converted = Str::upper('laravel');
// "LARAVEL"
Str::ulid() {.collection-method}
ulid 方法生成一個 ULID 實例:
use Illuminate\Support\Str;
(Str::ulid())->toString(); // "01H3FRNM1K5GQ87E4C0G5A908R"
Str::unwrap() {.collection-method}
Str::unwrap 方法從字串的開頭和結尾移除給定的值:
use Illuminate\Support\Str;
Str::unwrap('-Laravel-', '-'); // 'Laravel'
Str::unwrap('<div>Laravel</div>', '<div>'); // 'Laravel</div>'
Str::uuid() {.collection-method}
uuid 方法生成一個 UUIDv4(隨機)實例:
use Illuminate\Support\Str;
(Str::uuid())->toString(); // "5b9e3467-2c4f-4d5a-b9b5-4f6f7f8f9d0a"
Str::uuid7() {.collection-method}
uuid7 方法生成一個可排序的 UUIDv7 實例。此 UUID 對於索引資料庫儲存非常有效,因為它們可以按字典順序排序:
use Illuminate\Support\Str;
(Str::uuid7())->toString(); // "01925262-c6ca-7c8a-a00c-078378149ad9"
Str::wordCount() {.collection-method}
Str::wordCount 方法計算字串中的單詞數量:
use Illuminate\Support\Str;
$count = Str::wordCount('Hello, world!');
// 2
Str::wordWrap() {.collection-method}
Str::wordWrap 方法在指定的字元寬度處用給定的斷行字元包裹字串:
use Illuminate\Support\Str;
$wrapped = Str::wordWrap('The quick brown fox jumps over the lazy dog.', 15, '<br>');
// "The quick brown<br>fox jumps over<br>the lazy dog."
Str::words() {.collection-method}
Str::words 方法返回字串中的給定數量的單詞:
use Illuminate\Support\Str;
$words = Str::words('Perfectly balanced, as all things should be.', 3);
// "Perfectly balanced, as"
您可以選擇將第三個參數傳遞給 words 方法,該參數將附加到字串的末尾:
use Illuminate\Support\Str;
$words = Str::words('Perfectly balanced, as all things should be.', 3, '>>>');
// "Perfectly balanced, as>>>"
Str::wrap() {.collection-method}
Str::wrap 方法將字串包裹在給定的字串中,如果尚未包裝:
use Illuminate\Support\Str;
Str::wrap('Laravel', '');
// "Laravel"
Str::wrap('Laravel', 'html');
// "htmlLaravelhtml"
Str::wrap('Laravel', '''');
// "'Laravel'"
str() {.collection-method}
str 函數返回一個流暢字串實例:
use function Illuminate\Support\str;
$string = str('This is a string');
$string = str()->replace('foo', 'bar')->ascii();
trans() {.collection-method}
trans 函數是 __() 的快捷方式:
echo trans('messages.welcome');
trans_choice() {.collection-method}
trans_choice 函數使用給定的數量翻譯給定的字串。您可以將此函數用於建立複數形式:
echo trans_choice(
'messages.apples',
10
);
您可以透過將陣列作為第二個參數傳遞來定義要在翻譯中使用的值:
echo trans_choice('messages.apples', 10, ['count' => 10]);
如果您的翻譯字串定義了數位複數形式,您可以將第三個參數作為預設的數字插值傳遞:
echo trans_choice('foo', 10, [], 'bar');
在翻譯中使用預設插值時,:count 將被預設數字插值替換:
'bar' => ':count bar|:count bars',
流暢字串
除了上述的靜態 Str:: 方法之外,Laravel 還提供了使用 str() 函數建構的流暢字串操作介面:
use function Illuminate\Support\str;
$string = str('This is a string')->replace('foo', 'bar')->ascii();
流暢字串方法的所有方法都可用於這些字串實例:
after() {.collection-method .first-collection-method}
after 方法返回在給定值之後的所有內容:
use function Illuminate\Support\str;
$string = str('This is my name')->after('This is');
// " my name"
afterLast() {.collection-method}
afterLast 方法返回在最後一次出現的給定值之後的所有內容:
use function Illuminate\Support\str;
$string = str('App\Http\Controllers\Controller')->afterLast('\\');
// "Controller"
apa() {.collection-method}
apa 方法根據 APA 指南將給定的字串轉換為標題大小寫:
use function Illuminate\Support\str;
$title = str('Creating A Project')->apa();
// "Creating a Project"
append() {.collection-method}
append 方法向字串附加一個或多個值:
use function Illuminate\Support\str;
$string = str('Taylor')->append(' Jack', ' Otwell');
// "Taylor Jack Otwell"
ascii() {.collection-method}
ascii 方法將嘗試將字串轉寫為 ASCII 值:
use function Illuminate\Support\str;
$string = str('û')->ascii();
// "u"
basename() {.collection-method}
basename 方法返回路徑的最後一個部分:
use function Illuminate\Support\str;
$string = str('/foo/bar/baz')->basename();
// "baz"
before() {.collection-method}
before 方法返回在給定值之前的所有內容:
use function Illuminate\Support\str;
$string = str('This is my name')->before('my name');
// "This is "
beforeLast() {.collection-method}
beforeLast 方法返回在最後一次出現的給定值之前的所有內容:
use function Illuminate\Support\str;
$string = str('This is my name')->beforeLast('is');
// "This "
between() {.collection-method}
between 方法返回兩個值之間的部分:
use function Illuminate\Support\str;
$string = str('This is my name')->between('This', 'name');
// " is my "
betweenFirst() {.collection-method}
betweenFirst 方法返回兩個值之間最小可能的部分:
use function Illuminate\Support\str;
$string = str('[a] bc [d]')->betweenFirst('[', ']');
// "a"
camel() {.collection-method}
camel 方法將給定的字串轉換為 camelCase:
use function Illuminate\Support\str;
$string = str('foo_bar')->camel();
// "fooBar"
charAt() {.collection-method}
charAt 方法返回指定索引處的字元。如果索引超出範圍,則返回 false:
use function Illuminate\Support\str;
$string = str('This is my name.')->charAt(6);
// "s"
classBasename() {.collection-method}
classBasename 方法返回給定類別的類別名稱,不包含類別的命名空間:
use function Illuminate\Support\str;
$class = str('Foo\Bar\Baz')->classBasename();
// "Baz"
chopStart() {.collection-method}
chopStart 方法僅在值出現在字串開頭時移除給定值的第一次出現:
use function Illuminate\Support\str;
$url = str('https://laravel.com')->chopStart('https://');
// "laravel.com"
chopEnd() {.collection-method}
chopEnd 方法僅在值出現在字串結尾時移除給定值的最後一次出現:
use function Illuminate\Support\str;
$url = str('app/Models/Photograph.php')->chopEnd('.php');
// "app/Models/Photograph"
contains() {.collection-method}
contains 方法判斷給定的字串是否包含給定的值:
use function Illuminate\Support\str;
$contains = str('This is my name')->contains('my');
// true
containsAll() {.collection-method}
containsAll 方法判斷給定的字串是否包含給定陣列中的所有值:
use function Illuminate\Support\str;
$containsAll = str('This is my name')->containsAll(['my', 'name']);
// true
decrypt() {.collection-method}
decrypt 方法解密給定的值:
use function Illuminate\Support\str;
$string = str($encryptedValue)->decrypt();
deduplicate() {.collection-method}
deduplicate 方法將給定字串中連續的字元實例替換為單個實例:
use function Illuminate\Support\str;
$result = str('The Laravel Framework')->deduplicate();
// "The Laravel Framework"
dirname() {.collection-method}
dirname 方法返回路徑的父目錄:
use function Illuminate\Support\str;
$string = str('/foo/bar/baz')->dirname();
// "/foo/bar"
doesntContain() {.collection-method}
doesntContain 方法判斷給定的字串是否不包含給定的值:
use function Illuminate\Support\str;
$doesntContain = str('This is name')->doesntContain('my');
// true
doesntEndWith() {.collection-method}
doesntEndWith 方法判斷給定的字串是否不以給定的值結尾:
use function Illuminate\Support\str;
$result = str('This is my name')->doesntEndWith('dog');
// true
doesntStartWith() {.collection-method}
doesntStartWith 方法判斷給定的字串是否不以給定的值開頭:
use function Illuminate\Support\str;
$result = str('This is my name')->doesntStartWith('That');
// true
encrypt() {.collection-method}
encrypt 方法加密給定的值:
use function Illuminate\Support\str;
$string = str($plainText)->encrypt();
endsWith() {.collection-method}
endsWith 方法判斷給定的字串是否以給定的值結尾:
use function Illuminate\Support\str;
$result = str('This is my name')->endsWith('name');
// true
exactly() {.collection-method}
exactly 方法判斷給定的字串是否與給定的值完全匹配:
use function Illuminate\Support\str;
$result = str('Laravel')->exactly('Laravel');
// true
$result = str('Laravel')->exactly('Taylor');
// false
excerpt() {.collection-method}
excerpt 方法從給定的字串中提取摘要,該摘要匹配字串中該片語的第一次出現:
use function Illuminate\Support\str;
$excerpt = str('This is my name')->excerpt('my', [
'radius' => 3
]);
// "...is my na..."
explode() {.collection-method}
explode 方法使用給定的定界符將字串拆分為陣列:
use function Illuminate\Support\str;
$array = str('foo bar baz')->explode(' ');
// ['foo', 'bar', 'baz']
finish() {.collection-method}
finish 方法在字串不以給定的值結尾時,向字串添加給定值的單個實例:
use function Illuminate\Support\str;
$adjusted = str('this/string')->finish('/');
// "this/string/"
fromBase64() {.collection-method}
fromBase64 方法解碼給定的 Base64 字串:
use function Illuminate\Support\str;
$decoded = str('TGFyYXZlbA==')->fromBase64();
// "Laravel"
hash() {.collection-method}
hash 方法使用 Bcrypt 雜湊給定的值:
use function Illuminate\Support\str;
$hashed = str('secret')->hash();
headline() {.collection-method}
headline 方法將由大小寫、連字號或下劃線分隔的字串轉換為以空格分隔的字串,每個單詞的首字母大寫:
use function Illuminate\Support\str;
$headline = str('steve_jobs')->headline();
// "Steve Jobs"
initials() {.collection-method}
initials 方法將返回給定字串的首字母縮寫,可選擇將其大寫:
use function Illuminate\Support\str;
$initials = str('taylor otwell')->initials();
// "to"
inlineMarkdown() {.collection-method}
inlineMarkdown 方法將給定的字串轉換為 HTML,使用聯盟通用標記,但保留內聯 Markdown:
use function Illuminate\Support\str;
$html = str('**Laravel**')->inlineMarkdown();
// "<strong>Laravel</strong>"
is() {.collection-method}
is 方法判斷給定的字串是否與給定的模式匹配:
use function Illuminate\Support\str;
$matches = str('foobar')->is('foo*');
// true
isAscii() {.collection-method}
isAscii 方法判斷給定的字串是否包含 ASCII 字元:
use function Illuminate\Support\str;
$result = str('Taylor')->isAscii();
// true
isEmpty() {.collection-method}
isEmpty 方法判斷給定的字串是否為空:
use function Illuminate\Support\str;
$result = str('')->isEmpty();
// true
isNotEmpty() {.collection-method}
isNotEmpty 方法判斷給定的字串是否不為空:
use function Illuminate\Support\str;
$result = str('Laravel')->isNotEmpty();
// true
isJson() {.collection-method}
isJson 方法判斷給定的字串是否是有效的 JSON:
use function Illuminate\Support\str;
$result = str('[1,2,3]')->isJson();
// true
isUlid() {.collection-method}
isUlid 方法判斷給定的字串是否是有效的 ULID:
use function Illuminate\Support\str;
$result = str('01gd4d3tgrrfqeda94gdbtdk5c')->isUlid();
// true
isUrl() {.collection-method}
isUrl 方法判斷給定的字串是否是有效的 URL:
use function Illuminate\Support\str;
$result = str('https://laravel.com')->isUrl();
// true
isUuid() {.collection-method}
isUuid 方法判斷給定的字串是否是有效的 UUID:
use function Illuminate\Support\str;
$result = str('5b9e3467-2c4f-4d5a-b9b5-4f6f7f8f9d0a')->isUuid();
// true
kebab() {.collection-method}
kebab 方法將給定的字串轉換為 kebab-case:
use function Illuminate\Support\str;
$string = str('fooBar')->kebab();
// "foo-bar"
lcfirst() {.collection-method}
lcfirst 方法將給定字串的第一個字元轉換為小寫:
use function Illuminate\Support\str;
$string = str('Hello World')->lcfirst();
// "hello World"
length() {.collection-method}
length 方法返回給定字串的長度:
use function Illuminate\Support\str;
$length = str('Laravel')->length();
// 6
limit() {.collection-method}
limit 方法將字串截斷為給定的最大長度:
use function Illuminate\Support\str;
$result = str('The quick brown fox jumps over the lazy dog.')->limit(20);
// "The quick brown fox..."
lower() {.collection-method}
lower 方法將給定的字串轉換為小寫:
use function Illuminate\Support\str;
$string = str('LARAVEL')->lower();
// "laravel"
markdown() {.collection-method}
markdown 方法將給定的字串轉換為 HTML,使用聯盟通用標記:
use function Illuminate\Support\str;
$html = str('# Laravel')->markdown();
// "<h1>Laravel</h1>"
mask() {.collection-method}
mask 方法遮罩字串的一部分,從給定長度的第二個位置開始:
use function Illuminate\Support\str;
$masked = str('[email protected]')->mask('*', 3);
// "***[email protected]"
match() {.collection-method}
match 方法返回給定正規表達式的匹配:
use function Illuminate\Support\str;
$match = str('foo bar')->match('/bar/');
// "bar"
matchAll() {.collection-method}
matchAll 方法返回一個包含給定正規表達式所有匹配的陣列:
use function Illuminate\Support\str;
$matches = str('bar foo bar')->matchAll('/bar/');
// [0 => ['bar', 'bar']]
isMatch() {.collection-method}
isMatch 方法判斷給定的字串是否匹配給定的正規表達式:
use function Illuminate\Support\str;
$result = str('laravel')->isMatch('/^lar/');
// true
newLine() {.collection-method}
newLine 方法在字串中添加換行字元:
use function Illuminate\Support\str;
$string = str('Taylor')->newLine()->alsoNewLine()->append('Otwell');
// "Taylor\n\nOtwell"
padBoth() {.collection-method}
padBoth 方法使用給定的填充字串在字串的兩側填充,使其達到給定的長度:
use function Illuminate\Support\str;
$padded = str('Taylor')->padBoth(10, '_');
// "__Taylor__"
padLeft() {.collection-method}
padLeft 方法使用給定的填充字串在字串的左側填充,使其達到給定的長度:
use function Illuminate\Support\str;
$padded = str('Taylor')->padLeft(10, '0');
// "0000Taylor"
padRight() {.collection-method}
padRight 方法使用給定的填充字串在字串的右側填充,使其達到給定的長度:
use function Illuminate\Support\str;
$padded = str('Taylor')->padRight(10, '_');
// "Taylor____"
pipe() {.collection-method}
pipe 方法將字串傳遞給給定的回呼並返回結果:
use function Illuminate\Support\str;
$result = str('hello')->pipe(function ($string) {
return strtoupper($string);
});
// "HELLO"
plural() {.collection-method}
plural 方法將字串從單數轉換為複數:
use function Illuminate\Support\str;
$string = str('car')->plural();
// "cars"
position() {.collection-method}
position 方法返回子字串在給定字串中的位置:
use function Illuminate\Support\str;
$position = str('Hello, World!')->position('World');
// 7
prepend() {.collection-method}
prepend 方法在字串的開頭添加一個或多個值:
use function Illuminate\Support\str;
$string = str('Framework')->prepend('Laravel ');
// "Laravel Framework"
remove() {.collection-method}
remove 方法從字串中移除一個子字串:
use function Illuminate\Support\str;
$string = str('Peter Piper picked a peck of pickled peppers.')->remove(['pecker', 'pickled', 'piper']);
// "Peter Piper picked a peck of peppers."
repeat() {.collection-method}
repeat 方法重複給定的字串給定的次數:
use function Illuminate\Support\str;
$repeated = str('abc')->repeat(3);
// "abcabcabc"
replace() {.collection-method}
replace 方法替換給定的字串中的一個子字串:
use function Illuminate\Support\str;
$string = str('foo is the best?')->replace('bar', 'best');
// "foo is the bar?"
replaceArray() {.collection-method}
replaceArray 方法使用陣列中的值按順序替換給定字串中的佔位符:
use function Illuminate\Support\str;
$string = str('The event will take place on :first and :second')->replaceArray(
[':first', ':second'],
['November', 'December']
);
// "The event will take place on November and December"
replaceFirst() {.collection-method}
replaceFirst 方法替換給定字串中子字串的第一次出現:
use function Illuminate\Support\str;
$string = str('The quick brown fox jumps over the lazy dog.')->replaceFirst('the', 'a');
// "A quick brown fox jumps over the lazy dog."
replaceLast() {.collection-method}
replaceLast 方法替換給定字串中子字串的最後一次出現:
use function Illuminate\Support\str;
$string = str('The quick brown fox jumps over the lazy dog.')->replaceLast('the', 'a');
// "The quick brown fox jumps over a lazy dog."
replaceMatches() {.collection-method}
replaceMatches 方法替換給定字串中正規表達式的匹配:
use function Illuminate\Support\str;
$result = str('Taylor the Foobar')->replaceMatches('/[a-z]/', function (array $matches) {
return strtoupper($matches[0]);
});
// "TAYLOR THE FOOBAR"
replaceStart() {.collection-method}
replaceStart 方法僅在字串以給定值開頭時替換其第一次出現:
use function Illuminate\Support\str;
$replaced = str('Hello World')->replaceStart('Hello', 'Greetings');
// "Greetings World"
replaceEnd() {.collection-method}
replaceEnd 方法僅在字串以給定值結尾時替換其最後一次出現:
use function Illuminate\Support\str;
$replaced = str('Hello World')->replaceEnd('World', 'Laravel');
// "Hello Laravel"
scan() {.collection-method}
scan 方法使用 sprintf 語法解析字串:
use function Illuminate\Support\str;
[$name, $year] = str('2024 - Taylor Otwell')->scan('%d - %s');
// ['Taylor Otwell', '2024']
singular() {.collection-method}
singular 方法將字串從複數轉換為單數:
use function Illuminate\Support\str;
$string = str('cars')->singular();
// "car"
slug() {.collection-method}
slug 方法生成一個適合放在 URL 中的給定字串的簡潔表示:
use function Illuminate\Support\str;
$slug = str('Laravel Framework')->slug();
// "laravel-framework"
snake() {.collection-method}
snake 方法將給定的字串轉換為 snake_case:
use function Illuminate\Support\str;
$string = str('fooBar')->snake();
// "foo_bar"
split() {.collection-method}
split 方法使用正規表達式將字串拆分為陣列:
use function Illuminate\Support\str;
$segments = str('one, two, three')->split('/,\s*/');
// ['one', 'two', 'three']
squish() {.collection-method}
squish 方法壓縮給定字串中的所有空白:
use function Illuminate\Support\str;
$squished = str(' laravel framework ')->squish();
// "laravel framework"
start() {.collection-method}
start 方法在字串的開頭添加給定的值,如果它尚未以給定的值開頭:
use function Illuminate\Support\str;
$adjusted = str('this-is-a-string')->start('this-is-a-');
// "this-is-a-string"
startsWith() {.collection-method}
startsWith 方法判斷給定的字串是否以另一個子字串開頭:
use function Illuminate\Support\str;
$result = str('This is my name')->startsWith('This');
// true
stripTags() {.collection-method}
stripTags 方法從給定的字串中移除 HTML 標籤:
use function Illuminate\Support\str;
$string = str('<h1>Laravel</h1>')->stripTags();
// "Laravel"
studly() {.collection-method}
studly 方法將給定的字串轉換為大寫蛇形命名法:
use function Illuminate\Support\str;
$string = str('foo_bar')->studly();
// "FooBar"
substr() {.collection-method}
substr 方法返回字串的給定部分:
use function Illuminate\Support\str;
$string = str('Laravel 13')->substr(0, 6);
// "Laravel"
substrReplace() {.collection-method}
substrReplace 方法替換字串的給定部分:
use function Illuminate\Support\str;
$string = str('The Laravel Framework')->substrReplace('Framework', 10);
// "The Laravel Framework"
swap() {.collection-method}
swap 方法使用陣列中提供的鍵值對替換字串中的多個值:
use function Illuminate\Support\str;
$string = str('Tacos are great!')->swap([
'Tacos' => 'Burritos',
'great' => 'fantastic',
]);
// "Burritos are fantastic!"
take() {.collection-method}
take 方法返回字串的給定部分:
use function Illuminate\Support\str;
$string = str('The quick brown fox jumps over the lazy dog.')->take(5);
// "The q"
tap() {.collection-method}
tap 方法將字串傳遞給給定的回呼並返回字串實例:
use function Illuminate\Support\str;
$string = str('Hello')->tap(function ($string) {
logger($string);
});
// "Hello"
test() {.collection-method}
test 方法判斷給定的字串是否匹配給定的正規表達式:
use function Illuminate\Support\str;
$result = str('laravel')->test('/^lar/');
// true
title() {.collection-method}
title 方法將給定的字串轉換為標題大小寫:
use function Illuminate\Support\str;
$title = str('a nice title uses the correct case')->title();
// "A Nice Title Uses The Correct Case"
toBase64() {.collection-method}
toBase64 方法將給定的字串編碼為 Base64:
use function Illuminate\Support\str;
$encoded = str('Laravel')->toBase64();
// "TGFyYXZlbA=="
toHtmlString() {.collection-method}
toHtmlString 方法將字串轉換為 HtmlString 實例:
use Illuminate\Support\HtmlString;
$htmlString = str('Laravel</h1>')->toHtmlString();
toUri() {.collection-method}
toUri 方法將字串轉換為 Uri 實例:
$uri = str('https://laravel.com')->toUri();
transliterate() {.collection-method}
transliterate 方法將字串轉寫為與 ASCII 相似的字元:
use function Illuminate\Support\str;
$transliterated = str('Über')->transliterate();
// "Uber"
trim() {.collection-method}
trim 方法修剪字串兩端的空白字元和其他字元:
use function Illuminate\Support\str;
$string = str(' Laravel ')->trim();
// "Laravel"
ltrim() {.collection-method}
ltrim 方法修剪字串左側的空白字元和其他字元:
use function Illuminate\Support\str;
$string = str(' Laravel ')->ltrim();
// "Laravel "
rtrim() {.collection-method}
rtrim 方法修剪字串右側的空白字元和其他字元:
use function Illuminate\Support\str;
$string = str(' Laravel ')->rtrim();
// " Laravel"
ucfirst() {.collection-method}
ucfirst 方法將給定字串的第一個字元轉換為大寫:
use function Illuminate\Support\str;
$string = str('hello world')->ucfirst();
// "Hello world"
ucsplit() {.collection-method}
ucsplit 方法將給定的字串按大寫字元拆分為陣列:
use function Illuminate\Support\str;
$splits = str('fooBar')->ucsplit();
// [0 => 'foo', 1 => 'Bar']
ucwords() {.collection-method}
ucwords 方法將給定字串中每個單詞的首字母轉換為大寫:
use function Illuminate\Support\str;
$string = str('hello world')->ucwords();
// "Hello World"
unwrap() {.collection-method}
unwrap 方法從字串的開頭和結尾移除給定的值:
use function Illuminate\Support\str;
$string = str('-Laravel-')->unwrap('-');
// "Laravel"
upper() {.collection-method}
upper 方法將給定的字串轉換為大寫:
use function Illuminate\Support\str;
$string = str('laravel')->upper();
// "LARAVEL"
when() {.collection-method}
when 方法在給定的布林條件為 true 時對字串實例執行給定的回呼:
use function Illuminate\Support\str;
$string = str('Laravel')->when(true, function ($string) {
return strtoupper($string);
});
// "LARAVEL"
whenContains() {.collection-method}
whenContains 方法在字串包含給定值時對字串實例執行給定的回呼:
use function Illuminate\Support\str;
$string = str('Laravel Framework')->whenContains('Framework', function ($string) {
return strtoupper($string);
});
// "LARAVEL FRAMEWORK"
whenContainsAll() {.collection-method}
whenContainsAll 方法在字串包含所有給定值時對字串實例執行給定的回呼:
use function Illuminate\Support\str;
$string = str('Laravel Framework')->whenContainsAll(['Laravel', 'Framework'], function ($string) {
return strtoupper($string);
});
// "LARAVEL FRAMEWORK"
whenDoesntEndWith() {.collection-method}
whenDoesntEndWith 方法在字串不以給定值結尾時對字串實例執行給定的回呼:
use function Illuminate\Support\str;
$string = str('Laravel')->whenDoesntEndWith('foo', function ($string) {
return strtoupper($string);
});
// "LARAVEL"
whenDoesntStartWith() {.collection-method}
whenDoesntStartWith 方法在字串不以給定值開頭時對字串實例執行給定的回呼:
use function Illuminate\Support\str;
$string = str('Laravel')->whenDoesntStartWith('foo', function ($string) {
return strtoupper($string);
});
// "LARAVEL"
whenEmpty() {.collection-method}
whenEmpty 方法在字串為空時對字串實例執行給定的回呼:
use function Illuminate\Support\str;
$string = str('')->whenEmpty(function ($string) {
return strtoupper($string);
});
// ""
whenNotEmpty() {.collection-method}
whenNotEmpty 方法在字串不為空時對字串實例執行給定的回呼:
use function Illuminate\Support\str;
$string = str('Laravel')->whenNotEmpty(function ($string) {
return strtoupper($string);
});
// "LARAVEL"
whenStartsWith() {.collection-method}
whenStartsWith 方法在字串以給定值開頭時對字串實例執行給定的回呼:
use function Illuminate\Support\str;
$string = str('Laravel Framework')->whenStartsWith('Laravel', function ($string) {
return strtoupper($string);
});
// "LARAVEL FRAMEWORK"
whenEndsWith() {.collection-method}
whenEndsWith 方法在字串以給定值結尾時對字串實例執行給定的回呼:
use function Illuminate\Support\str;
$string = str('Laravel')->whenEndsWith('el', function ($string) {
return strtoupper($string);
});
// "LARAVEL"
whenExactly() {.collection-method}
whenExactly 方法在字串與給定值完全匹配時對字串實例執行給定的回呼:
use function Illuminate\Support\str;
$string = str('Laravel')->whenExactly('Laravel', function ($string) {
return strtoupper($string);
});
// "LARAVEL"
whenNotExactly() {.collection-method}
whenNotExactly 方法在字串與給定值不完全匹配時對字串實例執行給定的回呼:
use function Illuminate\Support\str;
$string = str('Laravel')->whenNotExactly('Taylor', function ($string) {
return strtoupper($string);
});
// "LARAVEL"
whenIs() {.collection-method}
whenIs 方法在字串匹配給定模式時對字串實例執行給定的回呼:
use function Illuminate\Support\str;
$string = str('Laravel Framework')->whenIs('Laravel *', function ($string) {
return strtoupper($string);
});
// "LARAVEL FRAMEWORK"
whenIsAscii() {.collection-method}
whenIsAscii 方法在字串僅包含 ASCII 字元時對字串實例執行給定的回呼:
use function Illuminate\Support\str;
$string = str('Taylor')->whenIsAscii(function ($string) {
return strtoupper($string);
});
// "TAYLOR"
whenIsUlid() {.collection-method}
whenIsUlid 方法在字串是有效 ULID 時對字串實例執行給定的回呼:
use function Illuminate\Support\str;
$string = str('01gd4d3tgrrfqeda94gdbtdk5c')->whenIsUlid(function ($string) {
return strtoupper($string);
});
// "01GD4D3TGRRFQEDA94GDBTDK5C"
whenIsUuid() {.collection-method}
whenIsUuid 方法在字串是有效 UUID 時對字串實例執行給定的回呼:
use function Illuminate\Support\str;
$string = str('5b9e3467-2c4f-4d5a-b9b5-4f6f7f8f9d0a')->whenIsUuid(function ($string) {
return strtoupper($string);
});
// "5B9E3467-2C4F-4D5A-B9B5-4F6F7F8F9D0A"
whenTest() {.collection-method}
whenTest 方法在字串匹配給定正規表達式時對字串實例執行給定的回呼:
use function Illuminate\Support\str;
$string = str('laravel')->whenTest('/^lar/', function ($string) {
return strtoupper($string);
});
// "LARAVEL"
wordCount() {.collection-method}
wordCount 方法計算字串中的單詞數量:
use function Illuminate\Support\str;
$count = str('Hello, world!')->wordCount();
// 2
words() {.collection-method}
words 方法返回字串中的給定數量的單詞:
use function Illuminate\Support\str;
$string = str('Perfectly balanced, as all things should be.')->words(3);
// "Perfectly balanced, as"
wrap() {.collection-method}
wrap 方法將字串包裹在給定的字串中,如果尚未包裝:
use function Illuminate\Support\str;
$string = str('Laravel')->wrap('''');
// "'Laravel'"