문자열
번역일: 2026년 6월 20일
문자열
소개
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
Fluent 문자열 메서드
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 __('애플리케이션에 오신 것을 환영합니다');
echo __('messages.welcome');지정한 번역 문자열이나 키가 존재하지 않으면, __ 함수는 전달한 값을 그대로 반환합니다. 위 예시에서 번역 키 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 = '이벤트는 :start 부터 :end 까지 진행됩니다';
$replaced = preg_replace_array('/:[a-z_]+/', ['8:30', '9:00'], $string);
// 이벤트는 8:30 부터 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']);
// trueignoreCase 인수를 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']);
// trueignoreCase 인수를 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']);
// trueignoreCase 인수를 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배열을 전달하면 문자열이 배열의 값 중 어느 것으로도 시작하지 않는지 확인합니다:
$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<a name="method-str-