문자열
번역일: 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::contains Str::containsAll Str::endsWith Str::excerpt Str::finish Str::headline 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::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::toHtmlString Str::ucfirst Str::ucsplit Str::upper Str::ulid Str::unwrap Str::uuid 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 contains containsAll dirname endsWith excerpt exactly explode finish headline inlineMarkdown is isAscii isEmpty isNotEmpty isJson isUlid isUrl isUuid kebab lcfirst length limit lower ltrim markdown mask match matchAll isMatch newLine padBoth padLeft padRight pipe plural position prepend remove repeat replace replaceArray replaceFirst replaceLast replaceMatches replaceStart replaceEnd rtrim scan singular slug snake split squish start startsWith stripTags studly substr substrReplace swap take tap test title toBase64 trim ucfirst ucsplit unwrap upper when whenContains whenContainsAll whenEmpty whenNotEmpty whenStartsWith whenEndsWith whenExactly whenNotExactly whenIs whenIsAscii whenIsUlid whenIsUuid whenTest wordCount words
문자열 함수
`__()` {.collection-method}
__ 함수는 지정된 번역 문자열 또는 번역 키를 언어 파일을 사용해 번역합니다:
echo __('애플리케이션에 오신 것을 환영합니다');
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 = '행사는 :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::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`Str::containsAll()` {.collection-method}
Str::containsAll 메서드는 문자열이 배열의 모든 값을 포함하는지 확인합니다:
use Illuminate\Support\Str;
$containsAll = Str::containsAll('This is my name', ['my', 'name']);
// 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::headline()` {.collection-method}
Str::headline 메서드는 대소문자, 하이픈, 언더스코어로 구분된 문자열을 각 단어의 첫 글자가 대문자인 공백 구분 문자열로 변환합니다:
use Illuminate\Support\Str;
$headline = Str::headline('steve_jobs');
// Steve Jobs
$headline = Str::headline('EmailNotificationSent');
// Email Notification Sent`Str::inlineMarkdown()` {.collection-method}
Str::inlineMarkdown 메서드는 GitHub 방식의 Markdown을 CommonMark를 사용해 인라인 HTML로 변환합니다. markdown 메서드와 달리 생성된 HTML 전체를 블록 레벨 요소로 감싸지 않습니다:
use Illuminate\Support\Str;
$html = Str::inlineMarkdown('**Laravel**');
// <strong>Laravel</strong>Markdown 보안 주의사항
Markdown은 기본적으로 원시 HTML을 허용하므로, 사용자 입력을 그대로 사용할 경우 XSS(크로스 사이트 스크립팅) 취약점에 노출될 수 있습니다. CommonMark 보안 문서에 따라 html_input 옵션으로 원시 HTML을 이스케이프하거나 제거하고, allow_unsafe_links 옵션으로 안전하지 않은 링크 허용 여부를 지정할 수 있습니다. 일부 원시 HTML을 허용해야 한다면 컴파일된 Markdown을 HTML Purifier를 통해 처리하세요:
use Illuminate\Support\Str;
Str::inlineMarkdown('주입 시도: <script>alert("Hello XSS!");</script>', [
'html_input' => 'strip',
'allow_unsafe_links' => false,
]);
// 주입 시도: alert("Hello XSS!");`Str::is()` {.collection-method}
Str::is 메서드는 문자열이 지정된 패턴과 일치하는지 확인합니다. 와일드카드로 별표(*)를 사용할 수 있습니다:
use Illuminate\Support\Str;
$matches = Str::is('foo*', 'foobar');
// true
$matches = Str::is('baz*', 'foobar');
// false`Str::isAscii()` {.collection-method}
Str::isAscii 메서드는 문자열이 7비트 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('{"first": "John", "last": "Doe"}');
// true
$result = Str::isJson('{first: "John", last: "Doe"}');
// false`Str::isUrl()` {.collection-method}
Str::isUrl 메서드는 문자열이 유효한 URL인지 확인합니다:
use Illuminate\Support\Str;
$isUrl = Str::isUrl('http://example.com');
// true
$isUrl = Str::isUrl('laravel');
// falseisUrl 메서드는 다양한 프로토콜을 유효한 것으로 간주합니다. 유효하게 인정할 프로토콜을 직접 지정할 수도 있습니다:
$isUrl = Str::isUrl('http://example.com', ['http', 'https']);
`Str::isUlid()` {.collection-method}
Str::isUlid 메서드는 문자열이 유효한 ULID인지 확인합니다:
use Illuminate\Support\Str;
$isUlid = Str::isUlid('01gd6r360bp37zj17nxb55yv40');
// true
$isUlid = Str::isUlid('laravel');
// false`Str::isUuid()` {.collection-method}
Str::isUuid 메서드는 문자열이 유효한 UUID인지 확인합니다:
use Illuminate\Support\Str;
$isUuid = Str::isUuid('a0a2a2d2-0b87-4a18-83f2-2529882be2de');
// true
$isUuid = 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;
$string = Str::lcfirst('Foo Bar');
// foo Bar`Str::length()` {.collection-method}
Str::length 메서드는 문자열의 길이를 반환합니다:
use Illuminate\Support\Str;
$length = Str::length('Laravel');
// 7`Str::limit()` {.collection-method}
Str::limit 메서드는 문자열을 지정된 길이로 잘라냅니다:
use Illuminate\Support\Str;
$truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20);
// The quick brown fox...세 번째 인수로 잘린 문자열 끝에 추가할 문자열을 지정할 수 있습니다:
use Illuminate\Support\Str;
$truncated = 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 메서드는 GitHub 방식의 Markdown을 CommonMark를 사용해 HTML로 변환합니다:
use Illuminate\Support\Str;
$html = Str::markdown('# Laravel');
// <h1>Laravel</h1>
$html = Str::markdown('# Taylor <b>Otwell</b>', [
'html_input' => 'strip',
]);
// <h1>Taylor Otwell</h1>Markdown 보안 주의사항
Markdown은 기본적으로 원시 HTML을 허용하므로, 사용자 입력을 그대로 사용할 경우