Skip to content

Strings

Giới thiệu (Introduction)

Laravel bao gồm nhiều functions để thao tác strings. Nhiều trong số này được framework sử dụng nội bộ nhưng bạn hoàn toàn có thể dùng trong ứng dụng.

Các Methods có sẵn (Available Methods)

Str::after()

php
use Illuminate\Support\Str;

Str::after('This is my name', 'This is');
// ' my name'

Str::afterLast()

php
Str::afterLast('App\Http\Controllers\Controller', '\\');
// 'Controller'

Str::before() / Str::beforeLast()

php
Str::before('This is my name', 'my name');
// 'This is '

Str::beforeLast('This is my name', 'is');
// 'This '

Str::between()

php
Str::between('This is my name', 'This', 'name');
// ' is my '

Str::camel() / Str::kebab() / Str::snake() / Str::studly()

php
Str::camel('foo_bar');     // 'fooBar'
Str::kebab('fooBar');      // 'foo-bar'
Str::snake('fooBar');      // 'foo_bar'
Str::studly('foo_bar');    // 'FooBar'

Str::contains() / Str::containsAll()

php
Str::contains('This is my name', 'my');       // true
Str::containsAll('This is my name', ['my', 'name']); // true

Str::endsWith() / Str::startsWith()

php
Str::endsWith('This is my name', 'name');     // true
Str::startsWith('This is my name', 'This');   // true

Str::headline()

php
Str::headline('steve_jobs');      // 'Steve Jobs'
Str::headline('EmailNotification'); // 'Email Notification'

Str::is() (Pattern matching)

php
Str::is('foo*', 'foobar');  // true
Str::is('baz*', 'foobar');  // false

Str::length() / Str::limit() / Str::words()

php
Str::length('Laravel');          // 7
Str::limit('The quick brown fox', 20); // 'The quick brown fox...'
Str::words('The quick brown', 2); // 'The quick...'

Str::lower() / Str::upper() / Str::title() / Str::ucfirst()

php
Str::lower('LARAVEL');     // 'laravel'
Str::upper('laravel');     // 'LARAVEL'
Str::title('a nice day');  // 'A Nice Day'
Str::ucfirst('foo bar');   // 'Foo bar'

Str::mask()

php
Str::mask('taylor@example.com', '*', 3);
// 'tay***************'

Str::orderedUuid() / Str::uuid()

php
(string) Str::uuid();         // UUID v4
(string) Str::orderedUuid();  // Timestamp-first UUID

Str::padBoth() / Str::padLeft() / Str::padRight()

php
Str::padBoth('James', 10, '_');  // '__James___'
Str::padLeft('James', 10, '-'); // '-----James'
Str::padRight('James', 10);    // 'James     '

Str::plural() / Str::singular()

php
Str::plural('car');     // 'cars'
Str::plural('child');   // 'children'
Str::singular('cars');  // 'car'

Str::random()

php
$random = Str::random(40);

Str::replace() / Str::replaceFirst() / Str::replaceLast()

php
Str::replace('9.x', '10.x', 'Laravel 9.x'); // 'Laravel 10.x'

Str::slug()

php
Str::slug('Laravel 13 Framework', '-');
// 'laravel-13-framework'

Str::substr()

php
Str::substr('Laravel Framework', 0, 7);
// 'Laravel'

Fluent Strings

Chain methods với Str::of() hoặc str():

php
use Illuminate\Support\Str;

$string = Str::of('  laravel  framework  ')
    ->trim()
    ->title()
    ->replace('Framework', 'Docs');
// 'Laravel Docs'

// Hoặc helper
$string = str('taylor')
    ->append(' otwell')
    ->title();
// 'Taylor Otwell'

Các Fluent Methods phổ biến

php
str('hello world')
    ->after('hello ')         // 'world'
    ->append('!')             // 'world!'
    ->upper();                // 'WORLD!'

str('foo bar baz')
    ->before(' baz')          // 'foo bar'
    ->camel();                // 'fooBar'

str('Laravel Framework')
    ->contains('Framework')    // true
    ->slug()                   // 'laravel-framework'
    ->toString();              // 'laravel-framework' (string)

str('Hello World')
    ->when(true, fn ($s) => $s->append('!'))
    ->toString();              // 'Hello World!'

str('the quick brown fox')
    ->wordCount();             // 4
    
str('Taylor')
    ->pipe('strtolower')       // 'taylor'
    ->pipe('ucfirst');         // 'Taylor'

Str::of() / str() — Kiểm tra

php
$string = str('Laravel');

$string->isEmpty();         // false
$string->isNotEmpty();      // true
$string->test('/^Lar/');    // true (regex)
$string->is('Lar*');        // true (pattern)
$string->exactly('Laravel'); // true