문자열 (Strings)
소개
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::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 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
문자열
__()
__ 함수는 언어 파일을 사용하여 주어진 번역 문자열이나 번역 키를 변환합니다.
echo __('Welcome to our application');
echo __('messages.welcome');
지정한 번역 문자열이나 키가 없을 경우, __ 함수는 전달한 값을 그대로 반환합니다. 위 예시의 경우, 해당 번역 키가 없으면 messages.welcome이 반환됩니다.
class_basename()
class_basename 함수는 클래스 네임스페이스를 제거한 뒤, 주어진 클래스의 클래스명만 반환합니다.
$class = class_basename('Foo\Bar\Baz');
// Baz
e()
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:00
Str::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);
// true
Str::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);
// true
Str::doesntContain()
Str::doesntContain 메서드는 주어진 문자열이 해당 값을 포함하지 않는지 확인합니다. 기본적으로 대소문자를 구분합니다.
use Illuminate\Support\Str;
$doesntContain = Str::doesntContain('This is name', 'my');
// true
배열을 사용할 경우, 문자열이 배열 내 어떤 값도 포함하지 않으면 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()
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()
Str::doesntEndWith 메서드는 주어진 문자열이 특정 값으로 끝나지 않는지 확인합니다.
use Illuminate\Support\Str;
$result = Str::doesntEndWith('This is my name', 'dog');
// true
배열을 사용할 경우, 문자열이 배열 내 어떤 값으로도 끝나지 않으면 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()
Str::doesntStartWith 메서드는 주어진 문자열이 특정 값으로 시작하지 않는지 확인합니다.
use Illuminate\Support\Str;
$result = Str::doesntStartWith('This is my name', 'That');
// true
배열을 사용할 경우, 문자열이 배열 내 어떤 값으로도 시작하지 않으면 true를 반환합니다.
$result = Str::doesntStartWith('This is my name', ['What', 'That', 'There']);
// true
Str::endsWith()
Str::endsWith 메서드는 주어진 문자열이 특정 값으로 끝나는지 확인합니다.
use Illuminate\Support\Str;
$result = Str::endsWith('This is my name', 'name');
// true
배열을 사용할 경우, 문자열이 배열 내 어떤 값으로 끝나면 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()
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/
Str::fromBase64()
Str::fromBase64 메서드는 주어진 Base64 문자열을 디코딩합니다.
use Illuminate\Support\Str;
$decoded = Str::fromBase64('TGFyYXZlbA==');
// Laravel
Str::headline()
Str::headline 메서드는 대소문자, 하이픈, 언더스코어 등으로 구분된 문자열을 각 단어의 첫 글자가 대문자인 띄어쓰기로 변환합니다.
use Illuminate\Support\Str;
$headline = Str::headline('steve_jobs');
// Steve Jobs
$headline = Str::headline('EmailNotificationSent');
// Email Notification Sent
Str::inlineMarkdown()
Str::inlineMarkdown 메서드는 GitHub 스타일의 마크다운을 CommonMark로 변환하여 인라인 HTML로 변경합니다. markdown 메서드와 달리, 생성된 모든 HTML을 블록 요소로 감싸지 않습니다.
use Illuminate\Support\Str;
$html = Str::inlineMarkdown('**Laravel**');
// <strong>Laravel</strong>
마크다운 보안
기본적으로 마크다운은 raw HTML을 지원하므로 사용자 입력이 포함될 경우 XSS(크로스 사이트 스크립팅) 취약점이 생길 수 있습니다. CommonMark 보안 문서에 따라, html_input 옵션으로 raw HTML의 escape 또는 strip 처리, allow_unsafe_links 옵션으로 안전하지 않은 링크 허용 여부를 지정할 수 있습니다. raw HTML의 일부만 허용하려면, 컴파일된 마크다운을 HTML Purifier에 통과시켜야 합니다.
use Illuminate\Support\Str;
Str::inlineMarkdown('Inject: <script>alert("Hello XSS!");</script>', [
'html_input' => 'strip',
'allow_unsafe_links' => false,
]);
// Inject: alert("Hello XSS!");
Str::is()
Str::is 메서드는 주어진 문자열이 지정한 패턴과 일치하는지 확인합니다. 별표(*)를 와일드카드로 사용할 수 있습니다.
use Illuminate\Support\Str;
$matches = Str::is('foo*', 'foobar');
// true
$matches = Str::is('baz*', 'foobar');
// false
ignoreCase 인수를 true로 지정하면 대소문자를 구분하지 않습니다.
use Illuminate\Support\Str;
$matches = Str::is('*.jpg', 'photo.JPG', ignoreCase: true);
// true
Str::isAscii()
Str::isAscii 메서드는 주어진 문자열이 7비트 ASCII로만 구성되어 있는지 확인합니다.
use Illuminate\Support\Str;
$isAscii = Str::isAscii('Taylor');
// true
$isAscii = Str::isAscii('ü');
// false
Str::isJson()
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()
Str::isUrl 메서드는 주어진 문자열이 유효한 URL인지 확인합니다.
use Illuminate\Support\Str;
$isUrl = Str::isUrl('http://example.com');
// true
$isUrl = Str::isUrl('laravel');
// false
이 메서드는 다양한 프로토콜을 유효하다고 판단합니다. 필요한 경우, 체크할 프로토콜을 전달하여 제한할 수 있습니다.
$isUrl = Str::isUrl('http://example.com', ['http', 'https']);
Str::isUlid()
Str::isUlid 메서드는 지정한 문자열이 유효한 ULID인지 확인합니다.
use Illuminate\Support\Str;
$isUlid = Str::isUlid('01gd6r360bp37zj17nxb55yv40');
// true
$isUlid = Str::isUlid('laravel');
// false
Str::isUuid()
Str::isUuid 메서드는 주어진 문자열이 유효한 UUID인지 확인합니다.
use Illuminate\Support\Str;
$isUuid = Str::isUuid('a0a2a2d2-0b87-4a18-83f2-2529882be2de');
// true
$isUuid = Str::isUuid('laravel');
// false
버전별(1, 3, 4, 5, 6, 7, 8) UUID 명세와 일치하는지 검증할 수도 있습니다.
use Illuminate\Support\Str;
$isUuid = Str::isUuid('a0a2a2d2-0b87-4a18-83f2-2529882be2de', version: 4);
// true
$isUuid = Str::isUuid('a0a2a2d2-0b87-4a18-83f2-2529882be2de', version: 1);
// false
Str::kebab()
Str::kebab 메서드는 지정한 문자열을 kebab-case로 변환합니다.
use Illuminate\Support\Str;
$converted = Str::kebab('fooBar');
// foo-bar
Str::lcfirst()
Str::lcfirst 메서드는 문자열의 첫 글자를 소문자로 바꿔 반환합니다.
use Illuminate\Support\Str;
$string = Str::lcfirst('Foo Bar');
// foo Bar
Str::length()
Str::length 메서드는 주어진 문자열의 길이를 반환합니다.
use Illuminate\Support\Str;
$length = Str::length('Laravel');
// 7
Str::limit()
Str::limit 메서드는 주어진 문자열을 지정한 길이만큼 잘라냅니다.
use Illuminate\Support\Str;
$truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20);
// The quick brown fox...
세 번째 인수를 사용해 잘려진 뒤에 붙일 문자열을 변경할 수 있습니다.
$truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20, ' (...)');
// The quick brown fox (...)
문자열을 자를 때 단어가 끊기지 않도록 하려면 preserveWords 인수를 true로 지정하세요. 이 경우, 가장 가까운 단어 경계까지 자르게 됩니다.
$truncated = Str::limit('The quick brown fox', 12, preserveWords: true);
// The quick...
Str::lower()
Str::lower 메서드는 지정한 문자열을 모두 소문자로 변환합니다.
use Illuminate\Support\Str;
$converted = Str::lower('LARAVEL');
// laravel
Str::markdown()
Str::markdown 메서드는 GitHub 스타일의 마크다운을 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>
마크다운 보안
마크다운은 기본적으로 raw HTML을 지원하므로, 사용자 입력과 함께 쓸 때는 XSS(크로스 사이트 스크립팅) 공격에 노출될 수 있습니다. CommonMark 보안 문서에 따라, html_input 옵션으로 raw HTML escape/제거, allow_unsafe_links로 안전하지 않은 링크 허용 여부를 지정할 수 있습니다. 일부 raw HTML만 허용하려면 HTML Purifier를 이용하세요.
use Illuminate\Support\Str;
Str::markdown('Inject: <script>alert("Hello XSS!");</script>', [
'html_input' => 'strip',
'allow_unsafe_links' => false,
]);
// <p>Inject: alert("Hello XSS!");</p>
Str::mask()
Str::mask 메서드는 문자열의 일부분을 반복되는 문자로 가립니다. 이메일, 전화번호처럼 민감한 부분을 숨길 때 유용합니다.
use Illuminate\Support\Str;
$string = Str::mask('[email protected]', '*', 3);
// tay***************
필요하다면 세 번째 인수로 음수를 줄 수도 있는데, 이 경우 문자열의 끝에서부터 그 거리부터 마스킹을 시작합니다.
$string = Str::mask('[email protected]', '*', -15, 3);
// tay***@example.com
Str::match()
Str::match 메서드는 지정한 정규식 패턴과 일치하는 문자열 부분을 반환합니다.
use Illuminate\Support\Str;
$result = Str::match('/bar/', 'foo bar');
// 'bar'
$result = Str::match('/foo (.*)/', 'foo bar');
// 'bar'
Str::matchAll()
Str::matchAll 메서드는 정규식 패턴에 일치하는 모든 부분을 컬렉션 형태로 반환합니다.
use Illuminate\Support\Str;
$result = Str::matchAll('/bar/', 'bar foo bar');
// collect(['bar', 'bar'])
그룹이 있을 경우, 첫 번째 그룹의 매칭 결과만 컬렉션으로 반환합니다.
use Illuminate\Support\Str;
$result = Str::matchAll('/f(\w*)/', 'bar fun bar fly');
// collect(['un', 'ly']);
일치하는 값이 없으면 빈 컬렉션이 반환됩니다.
Str::isMatch()
Str::isMatch 메서드는 문자열이 주어진 정규식 패턴과 일치하는지 여부를 true 또는 false로 반환합니다.
use Illuminate\Support\Str;
$result = Str::isMatch('/foo (.*)/', 'foo bar');
// true
$result = Str::isMatch('/foo (.*)/', 'laravel');
// false
Str::orderedUuid()
Str::orderedUuid 메서드는 인덱스 컬럼에 효율적으로 저장될 수 있는 "타임스탬프 우선" UUID를 생성합니다. 이전에 만든 UUID보다 나중에 생성된 UUID가 정렬상 더 뒤에 위치하게 됩니다.
use Illuminate\Support\Str;
return (string) Str::orderedUuid();
Str::padBoth()
Str::padBoth 메서드는 PHP의 str_pad 함수를 감싸 문자열 양쪽에 지정한 문자열로 패딩하여 특정 길이에 도달할 때까지 확장합니다.
use Illuminate\Support\Str;
$padded = Str::padBoth('James', 10, '_');
// '__James___'
$padded = Str::padBoth('James', 10);
// ' James '
Str::padLeft()
Str::padLeft 메서드는 PHP의 str_pad 함수를 감싸 문자열 왼쪽에 지정한 문자열로 패딩하여 특정 길이에 도달할 때까지 확장합니다.
use Illuminate\Support\Str;
$padded = Str::padLeft('James', 10, '-=');
// '-=-=-James'
$padded = Str::padLeft('James', 10);
// ' James'
Str::padRight()
Str::padRight 메서드는 PHP의 str_pad 함수를 감싸 문자열 오른쪽에 지정한 문자열로 패딩하여 특정 길이에 도달할 때까지 확장합니다.
use Illuminate\Support\Str;
$padded = Str::padRight('James', 10, '-');
// 'James-----'
$padded = Str::padRight('James', 10);
// 'James '
Str::password()
Str::password 메서드는 지정한 길이의 안전하고 랜덤한 비밀번호를 생성합니다. 비밀번호에는 영문, 숫자, 기호, 공백이 혼합됩니다. 기본 길이는 32자입니다.
use Illuminate\Support\Str;
$password = Str::password();
// 'EbJo2vE-AS:U,$%_gkrV4n,q~1xy/-_4'
$password = Str::password(12);
// 'qwuar>#V|i]N'
Str::plural()
Str::plural 메서드는 단수형 단어 문자열을 복수형으로 변환합니다. 이 함수는 Laravel의 다양한 복수화 언어를 지원합니다.
use Illuminate\Support\Str;
$plural = Str::plural('car');
// cars
$plural = Str::plural('child');
// children
두 번째 인수로 정수를 전달하여 단수/복수 여부를 직접 지정할 수 있습니다.
use Illuminate\Support\Str;
$plural = Str::plural('child', 2);
// children
$singular = Str::plural('child', 1);
// child
prependCount 인수를 전달하면, 복수형 문자열 앞에 $count 값을 붙일 수 있습니다.
use Illuminate\Support\Str;
$label = Str::plural('car', 1000, prependCount: true);
// 1,000 cars
Str::pluralStudly()
Str::pluralStudly 메서드는 studly caps 표기법(StudlyCase)으로 된 단어의 복수형 문자열로 변환합니다. 이 함수도 Laravel의 다양한 복수화 언어를 지원합니다.
use Illuminate\Support\Str;
$plural = Str::pluralStudly('VerifiedHuman');
// VerifiedHumans
$plural = Str::pluralStudly('UserFeedback');
// UserFeedback
두 번째 인수로 정수를 전달하여 단수/복수 여부를 직접 지정할 수 있습니다.
use Illuminate\Support\Str;
$plural = Str::pluralStudly('VerifiedHuman', 2);
// VerifiedHumans
$singular = Str::pluralStudly('VerifiedHuman', 1);
// VerifiedHuman
Str::position()
Str::position 메서드는 문자열 내에서 특정 서브스트링이 처음 등장하는 위치를 반환합니다. 해당 서브스트링이 없으면 false를 반환합니다.
use Illuminate\Support\Str;
$position = Str::position('Hello, World!', 'Hello');
// 0
$position = Str::position('Hello, World!', 'W');
// 7
Str::random()
Str::random 메서드는 지정한 길이의 랜덤 문자열을 생성합니다. 이 함수는 PHP의 random_bytes 함수를 사용합니다.
use Illuminate\Support\Str;
$random = Str::random(40);
테스트 시, Str::random의 반환 값을 임의로 지정할 수 있습니다. 이를 위해 createRandomStringsUsing 메서드를 사용하세요.
Str::createRandomStringsUsing(function () {
return 'fake-random-string';
});
랜덤 문자열 생성을 정상 모드로 돌리고 싶다면 createRandomStringsNormally 메서드를 호출합니다.
Str::createRandomStringsNormally();
Str::remove()
Str::remove 메서드는 문자열에서 지정한 값(혹은 값의 배열)을 제거합니다.
use Illuminate\Support\Str;
$string = 'Peter Piper picked a peck of pickled peppers.';
$removed = Str::remove('e', $string);
// Ptr Pipr pickd a pck of pickld ppprs.
세 번째 인수로 false를 전달하면 대소문자를 무시하고 제거할 수 있습니다.
Str::repeat()
Str::repeat 메서드는 문자열을 지정 횟수만큼 반복합니다.
use Illuminate\Support\Str;
$string = 'a';
$repeat = Str::repeat($string, 5);
// aaaaa
Str::replace()
Str::replace 메서드는 문자열 내의 특정 값을 대체합니다.
use Illuminate\Support\Str;
$string = 'Laravel 11.x';
$replaced = Str::replace('11.x', '12.x', $string);
// Laravel 12.x
replace 메서드는 caseSensitive 인수도 받을 수 있습니다. 기본적으로 대소문자를 구분합니다.
$replaced = Str::replace(
'php',
'Laravel',
'PHP Framework for Web Artisans',
caseSensitive: false
);
// Laravel Framework for Web Artisans
Str::replaceArray()
Str::replaceArray 메서드는 문자열 내의 값을 배열의 값 순서대로 차례로 대체합니다.
use Illuminate\Support\Str;
$string = 'The event will take place between ? and ?';
$replaced = Str::replaceArray('?', ['8:30', '9:00'], $string);
// The event will take place between 8:30 and 9:00
Str::replaceFirst()
Str::replaceFirst 메서드는 지정한 값이 처음 등장한 부분만 대체합니다.
use Illuminate\Support\Str;
$replaced = Str::replaceFirst('the', 'a', 'the quick brown fox jumps over the lazy dog');
// a quick brown fox jumps over the lazy dog
Str::replaceLast()
Str::replaceLast 메서드는 지정한 값이 마지막으로 등장한 부분만 대체합니다.
use Illuminate\Support\Str;
$replaced = Str::replaceLast('the', 'a', 'the quick brown fox jumps over the lazy dog');
// the quick brown fox jumps over a lazy dog
Str::replaceMatches()
Str::replaceMatches 메서드는 패턴과 일치하는 문자열의 모든 부분을 지정한 값으로 대체합니다.
use Illuminate\Support\Str;
$replaced = Str::replaceMatches(
pattern: '/[^A-Za-z0-9]++/',
replace: '',
subject: '(+1) 501-555-1000'
)
// '15015551000'
이 메서드는 각 패턴에 맞는 부분마다 클로저를 전달해 대체 로직을 커스터마이징할 수도 있습니다.
use Illuminate\Support\Str;
$replaced = Str::replaceMatches('/\d/', function (array $matches) {
return '['.$matches[0].']';
}, '123');
// '[1][2][3]'
Str::replaceStart()
Str::replaceStart 메서드는 주어진 값이 문자열의 처음에 있을 때만, 처음 등장하는 부분만 대체합니다.
use Illuminate\Support\Str;
$replaced = Str::replaceStart('Hello', 'Laravel', 'Hello World');
// Laravel World
$replaced = Str::replaceStart('World', 'Laravel', 'Hello World');
// Hello World
Str::replaceEnd()
Str::replaceEnd 메서드는 주어진 값이 문자열의 끝에 있을 때만, 마지막 등장하는 부분만 대체합니다.
use Illuminate\Support\Str;
$replaced = Str::replaceEnd('World', 'Laravel', 'Hello World');
// Hello Laravel
$replaced = Str::replaceEnd('Hello', 'Laravel', 'Hello World');
// Hello World
Str::reverse()
Str::reverse 메서드는 문자열의 순서를 뒤집어 반환합니다.
use Illuminate\Support\Str;
$reversed = Str::reverse('Hello World');
// dlroW olleH
Str::singular()
Str::singular 메서드는 문자열을 단수형으로 변환합니다. Laravel의 다양한 복수화 언어를 지원합니다.
use Illuminate\Support\Str;
$singular = Str::singular('cars');
// car
$singular = Str::singular('children');
// child
Str::slug()
Str::slug 메서드는 지정한 문자열을 URL 친화적인 "슬러그(slug)"로 변환합니다.
use Illuminate\Support\Str;
$slug = Str::slug('Laravel 5 Framework', '-');
// laravel-5-framework
Str::snake()
Str::snake 메서드는 문자열을 snake_case로 변환합니다.
use Illuminate\Support\Str;
$converted = Str::snake('fooBar');
// foo_bar
$converted = Str::snake('fooBar', '-');
// foo-bar
Str::squish()
Str::squish 메서드는 문자열의 앞뒤 및 중간에 있는 불필요한 공백을 모두 제거합니다.
use Illuminate\Support\Str;
$string = Str::squish(' laravel framework ');
// laravel framework
Str::start()
Str::start 메서드는 주어진 값으로 문자열이 시작하지 않을 경우, 한 번만 해당 값을 맨 앞에 추가합니다.
use Illuminate\Support\Str;
$adjusted = Str::start('this/string', '/');
// /this/string
$adjusted = Str::start('/this/string', '/');
// /this/string
Str::startsWith()
Str::startsWith 메서드는 문자열이 지정한 값으로 시작하는지 확인합니다.
use Illuminate\Support\Str;
$result = Str::startsWith('This is my name', 'This');
// true
배열을 사용할 경우, 해당 배열의 값 중 하나라도 문자열의 시작에 있으면 true를 반환합니다.
$result = Str::startsWith('This is my name', ['This', 'That', 'There']);
// true
Str::studly()
Str::studly 메서드는 지정한 문자열을 StudlyCase로 변환합니다.
use Illuminate\Support\Str;
$converted = Str::studly('foo_bar');
// FooBar
Str::substr()
Str::substr 메서드는 시작 위치와 길이를 기준으로 지정한 부분 문자열을 반환합니다.
use Illuminate\Support\Str;
$converted = Str::substr('The Laravel Framework', 4, 7);
// Laravel
Str::substrCount()
Str::substrCount 메서드는 주어진 문자열 안에 특정 값이 몇 번 나타나는지 반환합니다.
use Illuminate\Support\Str;
$count = Str::substrCount('If you like ice cream, you will like snow cones.', 'like');
// 2
Str::substrReplace()
Str::substrReplace 메서드는 시작 위치와 교체할 길이를 지정하여 부분 문자열을 대체합니다. 마지막 인수에 0을 넘기면 해당 위치에 문자열을 삽입할 뿐 기존 문자는 지우지 않습니다.
use Illuminate\Support\Str;
$result = Str::substrReplace('1300', ':', 2);
// 13:
$result = Str::substrReplace('1300', ':', 2, 0);
// 13:00
Str::swap()
Str::swap 메서드는 문자열 내 여러 값을 PHP의 strtr 함수를 이용해 한 번에 교체합니다.
use Illuminate\Support\Str;
$string = Str::swap([
'Tacos' => 'Burritos',
'great' => 'fantastic',
], 'Tacos are great!');
// Burritos are fantastic!
Str::take()
Str::take 메서드는 문자열의 앞에서부터 지정한 수만큼의 문자를 반환합니다.
use Illuminate\Support\Str;
$taken = Str::take('Build something amazing!', 5);
// Build
Str::title()
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()
Str::toBase64 메서드는 주어진 문자열을 Base64로 변환합니다.
use Illuminate\Support\Str;
$base64 = Str::toBase64('Laravel');
// TGFyYXZlbA==
Str::transliterate()
Str::transliterate 메서드는 가능한 가장 비슷한 ASCII 문자로 변환을 시도합니다.
use Illuminate\Support\Str;
$email = Str::transliterate('ⓣⓔⓢⓣ@ⓛⓐⓡⓐⓥⓔⓛ.ⓒⓞⓜ');
// '[email protected]'
Str::trim()
Str::trim 메서드는 문자열의 시작과 끝에서 공백(또는 다른 문자)들을 제거합니다. PHP 기본 trim 함수와 달리, Unicode 공백 문자도 제거합니다.
use Illuminate\Support\Str;
$string = Str::trim(' foo bar ');
// 'foo bar'
Str::ltrim()
Str::ltrim 메서드는 문자열 앞쪽의 공백(또는 지정한 문자)을 모두 제거합니다. PHP 기본 ltrim 함수와 달리, Unicode 공백 문자도 포함합니다.
use Illuminate\Support\Str;
$string = Str::ltrim(' foo bar ');
// 'foo bar '
Str::rtrim()
Str::rtrim 메서드는 문자열 뒤쪽의 공백(또는 지정한 문자)을 모두 제거합니다. PHP 기본 rtrim 함수와 달리, Unicode 공백 문자도 포함합니다.
use Illuminate\Support\Str;
$string = Str::rtrim(' foo bar ');
// ' foo bar'
Str::ucfirst()
Str::ucfirst 메서드는 문자열의 첫 글자만 대문자로 만듭니다.
use Illuminate\Support\Str;
$string = Str::ucfirst('foo bar');
// Foo bar
Str::ucsplit()
Str::ucsplit 메서드는 대문자를 기준으로 문자열을 배열로 분리합니다.
use Illuminate\Support\Str;
$segments = Str::ucsplit('FooBar');
// [0 => 'Foo', 1 => 'Bar']
Str::ucwords()
Str::ucwords 메서드는 문자열의 각 단어의 첫 글자를 대문자로 변환합니다.
use Illuminate\Support\Str;
$string = Str::ucwords('laravel framework');
// Laravel Framework
Str::upper()
Str::upper 메서드는 문자열을 모두 대문자로 변환합니다.
use Illuminate\Support\Str;
$string = Str::upper('laravel');
// LARAVEL
Str::ulid()
Str::ulid 메서드는 ULID(Compact, 시간 순서에 따른 고유 ID)를 생성합니다.
use Illuminate\Support\Str;
return (string) Str::ulid();
// 01gd6r360bp37zj17nxb55yv40
ULID가 생성된 날짜 및 시간을 Illuminate\Support\Carbon의 인스턴스로 얻으려면 Carbon의 createFromId 메서드를 활용하세요.
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
$date = Carbon::createFromId((string) Str::ulid());
테스트 시, Str::ulid의 반환값을 임의로 지정하려면 createUlidsUsing 메서드를 사용하세요.
use Symfony\Component\Uid\Ulid;
Str::createUlidsUsing(function () {
return new Ulid('01HRDBNHHCKNW2AK4Z29SN82T9');
});
ULID 생성을 다시 정상 동작하도록 돌리려면 createUlidsNormally 메서드를 호출하면 됩니다.
Str::createUlidsNormally();
Str::unwrap()
Str::unwrap 메서드는 주어진 문자열의 시작과 끝에서 지정한 값을 제거합니다.
use Illuminate\Support\Str;
Str::unwrap('-Laravel-', '-');
// Laravel
Str::unwrap('{framework: "Laravel"}', '{', '}');
// framework: "Laravel"
Str::uuid()
Str::uuid 메서드는 버전 4 UUID를 생성합니다.
use Illuminate\Support\Str;
return (string) Str::uuid();
테스트 시, Str::uuid의 반환 값을 임의로 지정하려면 createUuidsUsing 메서드를 사용하세요.
use Ramsey\Uuid\Uuid;
Str::createUuidsUsing(function () {
return Uuid::fromString('eadbfeac-5258-45c2-bab7-ccb9b5ef74f9');
});
랜덤 UUID 생성을 다시 정상 동작으로 돌리려면 createUuidsNormally 메서드를 호출하세요.
Str::createUuidsNormally();
Str::uuid7()
Str::uuid7 메서드는 버전 7 UUID를 생성합니다.
use Illuminate\Support\Str;
return (string) Str::uuid7();
정렬된 UUID를 생성할 때, 선택적으로 DateTimeInterface 인스턴스를 전달할 수 있습니다.
return (string) Str::uuid7(time: now());
Str::wordCount()
Str::wordCount 메서드는 문자열이 몇 개의 단어로 구성되어 있는지 반환합니다.
use Illuminate\Support\Str;
Str::wordCount('Hello, world!'); // 2
Str::wordWrap()
Str::wordWrap 메서드는 문자열을 지정한 글자 수로 줄바꿈 처리합니다.
use Illuminate\Support\Str;
$text = "The quick brown fox jumped over the lazy dog."
Str::wordWrap($text, characters: 20, break: "<br />\n");
/*
The quick brown fox<br />
jumped over the lazy<br />
dog.
*/
Str::words()
Str::words 메서드는 문자열에서 지정한 수의 단어만 남기고 잘라냅니다. 잘린 뒤에 붙일 문자열은 세 번째 인수로 전달할 수 있습니다.
use Illuminate\Support\Str;
return Str::words('Perfectly balanced, as all things should be.', 3, ' >>>');
// Perfectly balanced, as >>>
Str::wrap()
Str::wrap 메서드는 문자열의 양끝을 지정한 문자(또는 쌍)로 감쌉니다.
use Illuminate\Support\Str;
Str::wrap('Laravel', '"');
// "Laravel"
Str::wrap('is', before: 'This ', after: ' Laravel!');
// This is Laravel!
str()
str 함수는 주어진 문자열로 새로운 Illuminate\Support\Stringable 인스턴스를 반환합니다. 이는 Str::of 메서드와 동일합니다.
$string = str('Taylor')->append(' Otwell');
// 'Taylor Otwell'
인자를 전달하지 않으면, Illuminate\Support\Str의 인스턴스를 반환합니다.
$snake = str()->snake('FooBar');
// 'foo_bar'
trans()
trans 함수는 언어 파일을 사용하여 주어진 번역 키를 변환합니다.
echo trans('messages.welcome');
지정한 번역 키가 존재하지 않을 경우, trans 함수는 키 자체를 반환합니다. 위 예시에서 해당 키가 없다면 messages.welcome이 반환됩니다.
trans_choice()
trans_choice 함수는 복수형 처리가 포함된 번역 키를 변환합니다.
echo trans_choice('messages.notifications', $unreadCount);
지정한 번역 키가 존재하지 않으면, trans_choice 함수 역시 키 자체를 반환합니다. 위 예시의 경우 해당 키가 없으면 messages.notifications이 반환됩니다.
플루언트 문자열(Fluent Strings)
플루언트 문자열은 문자열 값을 더 플루언트하고 객체지향적으로 다룰 수 있는 인터페이스를 제공합니다. 즉, 여러 문자열 연산을 연쇄적으로 연결하여 더 읽기 쉬운 구문으로 작업할 수 있습니다.
after
after 메서드는 지정한 값 뒤의 모든 문자열 부분을 반환합니다. 해당 값이 없으면 전체 문자열이 반환됩니다.
use Illuminate\Support\Str;
$slice = Str::of('This is my name')->after('This is');
// ' my name'
afterLast
afterLast 메서드는 지정한 값이 마지막으로 등장한 뒤의 모든 부분을 반환합니다. 값이 없으면 전체 문자열을 반환합니다.
use Illuminate\Support\Str;
$slice = Str::of('App\Http\Controllers\Controller')->afterLast('\\');
// 'Controller'
apa
apa 메서드는 문자열을 APA 가이드라인에 맞게 제목형으로 변환합니다.
use Illuminate\Support\Str;
$converted = Str::of('a nice title uses the correct case')->apa();
// A Nice Title Uses the Correct Case
append
append 메서드는 주어진 값을 문자열 끝에 붙입니다.
use Illuminate\Support\Str;
$string = Str::of('Taylor')->append(' Otwell');
// 'Taylor Otwell'
ascii
ascii 메서드는 가능한 경우 문자열을 ASCII로 변환하려고 시도합니다.
use Illuminate\Support\Str;
$string = Str::of('ü')->ascii();
// 'u'
basename
basename 메서드는 주어진 문자열의 마지막 부분(파일명 등)을 반환합니다.
use Illuminate\Support\Str;
$string = Str::of('/foo/bar/baz')->basename();
// 'baz'
필요하다면 제거할 확장자를 지정할 수 있습니다.
use Illuminate\Support\Str;
$string = Str::of('/foo/bar/baz.jpg')->basename('.jpg');
// 'baz'
before
before 메서드는 지정한 값 이전의 모든 부분을 반환합니다.
use Illuminate\Support\Str;
$slice = Str::of('This is my name')->before('my name');
// 'This is '
beforeLast
beforeLast 메서드는 지정한 값이 마지막으로 등장하기 전의 모든 부분을 반환합니다.
use Illuminate\Support\Str;
$slice = Str::of('This is my name')->beforeLast('is');
// 'This '
between
between 메서드는 두 값 사이의 문자열 부분을 반환합니다.
use Illuminate\Support\Str;
$converted = Str::of('This is my name')->between('This', 'name');
// ' is my '
betweenFirst
betweenFirst 메서드는 두 값 사이의 가장 작은 부분 문자열을 반환합니다.
use Illuminate\Support\Str;
$converted = Str::of('[a] bc [d]')->betweenFirst('[', ']');
// 'a'
camel
camel 메서드는 문자열을 camelCase로 변환합니다.
use Illuminate\Support\Str;
$converted = Str::of('foo_bar')->camel();
// 'fooBar'
charAt
charAt 메서드는 지정한 인덱스의 문자를 반환합니다. 범위를 벗어나면 false를 반환합니다.
use Illuminate\Support\Str;
$character = Str::of('This is my name.')->charAt(6);
// 's'
classBasename
classBasename 메서드는 네임스페이스를 제외한 클래스명을 반환합니다.
use Illuminate\Support\Str;
$class = Str::of('Foo\Bar\Baz')->classBasename();
// 'Baz'
chopStart
chopStart 메서드는 문자열이 지정 값으로 시작하는 경우, 첫 번째 등장하는 값을 제거합니다.
use Illuminate\Support\Str;
$url = Str::of('https://laravel.com')->chopStart('https://');
// 'laravel.com'
배열도 사용할 수 있습니다. 배열 내 값 중 하나로 시작하면 해당 부분이 제거됩니다.
use Illuminate\Support\Str;
$url = Str::of('http://laravel.com')->chopStart(['https://', 'http://']);
// 'laravel.com'
chopEnd
chopEnd 메서드는 문자열이 지정 값으로 끝나는 경우, 마지막 등장하는 값을 제거합니다.
use Illuminate\Support\Str;
$url = Str::of('https://laravel.com')->chopEnd('.com');
// 'https://laravel'
배열도 전달할 수 있으며, 배열 내 값 중 하나로 끝나면 해당 부분이 제거됩니다.
use Illuminate\Support\Str;
$url = Str::of('http://laravel.com')->chopEnd(['.com', '.io']);
// 'http://laravel'
contains
contains 메서드는 문자열이 주어진 값을 포함하는지 확인합니다. 기본적으로 대소문자를 구분합니다.
use Illuminate\Support\Str;
$contains = Str::of('This is my name')->contains('my');
// true
배열을 전달하면, 배열 내 어떤 값이 포함되어 있는지 확인합니다.
use Illuminate\Support\Str;
$contains = Str::of('This is my name')->contains(['my', 'foo']);
// true
ignoreCase 인수를 true로 설정하여 대소문자 구분을 해제할 수 있습니다.
use Illuminate\Support\Str;
$contains = Str::of('This is my name')->contains('MY', ignoreCase: true);
// true
containsAll
containsAll 메서드는 문자열이 배열 내 모든 값을 모두 포함하는지 확인합니다.
use Illuminate\Support\Str;
$containsAll = Str::of('This is my name')->containsAll(['my', 'name']);
// true
ignoreCase 인수를 true로 설정하여 대소문자 구분 없이 비교할 수 있습니다.
use Illuminate\Support\Str;
$containsAll = Str::of('This is my name')->containsAll(['MY', 'NAME'], ignoreCase: true);
// true
decrypt
decrypt 메서드는 암호화된 문자열을 복호화합니다.
use Illuminate\Support\Str;
$decrypted = $encrypted->decrypt();
// 'secret'
반대로 암호화를 원한다면 encrypt 메서드를 참고하세요.
deduplicate
deduplicate 메서드는 문자열에서 특정 문자의 연속된 부분을 한 번만 남기고 중복을 제거합니다. 기본값은 공백 문자입니다.
use Illuminate\Support\Str;
$result = Str::of('The Laravel Framework')->deduplicate();
// The Laravel Framework
두 번째 인자로 다른 문자를 지정할 수 있습니다.
use Illuminate\Support\Str;
$result = Str::of('The---Laravel---Framework')->deduplicate('-');
// The-Laravel-Framework
dirname
dirname 메서드는 주어진 문자열의 상위 디렉터리 부분을 반환합니다.
use Illuminate\Support\Str;
$string = Str::of('/foo/bar/baz')->dirname();
// '/foo/bar'
필요하면 잘라낼 디렉터리 레벨을 지정할 수 있습니다.
use Illuminate\Support\Str;
$string = Str::of('/foo/bar/baz')->dirname(2);
// '/foo'
doesntContain()
doesntContain 메서드는 문자열에 지정한 값이 포함되지 않는지 확인합니다. 이는 contains 메서드의 반대입니다. 기본적으로 대소문자를 구분합니다.
use Illuminate\Support\Str;
$doesntContain = Str::of('This is name')->doesntContain('my');
// true
배열을 사용할 경우, 배열 내 어떤 값도 포함되지 않으면 true를 반환합니다.
use Illuminate\Support\Str;
$doesntContain = Str::of('This is name')->doesntContain(['my', 'framework']);
// true
ignoreCase 인수를 true로 지정하여 대소문자 구분 없이 비교할 수 있습니다.
use Illuminate\Support\Str;
$doesntContain = Str::of('This is my name')->doesntContain('MY', ignoreCase: true);
// false
doesntEndWith
doesntEndWith 메서드는 문자열이 지정한 값으로 끝나지 않는지 확인합니다.
use Illuminate\Support\Str;
$result = Str::of('This is my name')->doesntEndWith('dog');
// true
배열을 사용하면 어느 값으로도 끝나지 않아야 true가 반환됩니다.
use Illuminate\Support\Str;
$result = Str::of('This is my name')->doesntEndWith(['this', 'foo']);
// true
$result = Str::of('This is my name')->doesntEndWith(['name', 'foo']);
// false
doesntStartWith
doesntStartWith 메서드는 문자열이 지정한 값으로 시작하지 않는지 확인합니다.
use Illuminate\Support\Str;
$result = Str::of('This is my name')->doesntStartWith('That');
// true
배열을 사용하면, 주어진 값들로 시작하지 않을 때에만 true가 반환됩니다.
use Illuminate\Support\Str;
$result = Str::of('This is my name')->doesntStartWith(['What', 'That', 'There']);
// true
encrypt
encrypt 메서드는 문자열을 암호화합니다.
use Illuminate\Support\Str;
$encrypted = Str::of('secret')->encrypt();
복호화를 원하면 decrypt 메서드를 참고하세요.
endsWith
endsWith 메서드는 문자열이 지정한 값으로 끝나는지 확인합니다.
use Illuminate\Support\Str;
$result = Str::of('This is my name')->endsWith('name');
// true
배열을 사용할 경우, 어느 하나로 끝나면 true를 반환합니다.
use Illuminate\Support\Str;
$result = Str::of('This is my name')->endsWith(['name', 'foo']);
// true
$result = Str::of('This is my name')->endsWith(['this', 'foo']);
// false
exactly
exactly 메서드는 문자열이 다른 문자열과 정확히 일치하는지 확인합니다.
use Illuminate\Support\Str;
$result = Str::of('Laravel')->exactly('Laravel');
// true
excerpt
excerpt 메서드는 특정 문구가 처음 등장한 주변을 기준으로 발췌문을 추출합니다.
use Illuminate\Support\Str;
$excerpt = Str::of('This is my name')->excerpt('my', [
'radius' => 3
]);
// '...is my na...'
radius(기본값: 100)는 양쪽에 나타날 문자의 수를 정의합니다. omission 옵션으로 생략 부분에 들어갈 문자를 지정할 수 있습니다.
use Illuminate\Support\Str;
$excerpt = Str::of('This is my name')->excerpt('name', [
'radius' => 3,
'omission' => '(...) '
]);
// '(...) my name'
explode
explode 메서드는 문자열을 지정 구분자로 쪼개 컬렉션에 담아 반환합니다.
use Illuminate\Support\Str;
$collection = Str::of('foo bar baz')->explode(' ');
// collect(['foo', 'bar', 'baz'])
finish
finish 메서드는 주어진 값으로 문자열이 끝나지 않으면 단 한 번만 그 값을 추가합니다.
use Illuminate\Support\Str;
$adjusted = Str::of('this/string')->finish('/');
// this/string/
$adjusted = Str::of('this/string/')->finish('/');
// this/string/
fromBase64
fromBase64 메서드는 Base64 문자열을 디코딩합니다.
use Illuminate\Support\Str;
$decoded = Str::of('TGFyYXZlbA==')->fromBase64();
// Laravel
hash
hash 메서드는 주어진 알고리즘으로 문자열을 해시처리합니다.
use Illuminate\Support\Str;
$hashed = Str::of('secret')->hash(algorithm: 'sha256');
// '2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b'
headline
headline 메서드는 대소문자, 하이픈, 언더스코어 등으로 나뉜 문자열을 각 단어의 첫 글자가 대문자인 띄어쓰기로 변환합니다.
use Illuminate\Support\Str;
$headline = Str::of('taylor_otwell')->headline();
// Taylor Otwell
$headline = Str::of('EmailNotificationSent')->headline();
// Email Notification Sent
inlineMarkdown
inlineMarkdown 메서드는 GitHub 스타일의 마크다운을 CommonMark를 사용해 인라인 HTML로 변환합니다. markdown과 달리 모든 결과를 블록 요소로 감싸지 않습니다.
use Illuminate\Support\Str;
$html = Str::of('**Laravel**')->inlineMarkdown();
// <strong>Laravel</strong>
마크다운 보안
마크다운은 raw HTML을 지원하므로 사용자 입력에 따라 XSS 취약점에 노출됩니다. CommonMark 보안 문서에 따라 html_input과 allow_unsafe_links 옵션을 활용해 보안을 강화할 수 있습니다. 일부 raw HTML만 허용하려면 HTML Purifier를 활용하세요.
use Illuminate\Support\Str;
Str::of('Inject: <script>alert("Hello XSS!");</script>')->inlineMarkdown([
'html_input' => 'strip',
'allow_unsafe_links' => false,
]);
// Inject: alert("Hello XSS!");
is
is 메서드는 문자열이 지정한 패턴과 일치하는지 확인합니다. 별표(*)로 와일드카드 사용이 가능합니다.
use Illuminate\Support\Str;
$matches = Str::of('foobar')->is('foo*');
// true
$matches = Str::of('foobar')->is('baz*');
// false
isAscii
isAscii 메서드는 문자열이 ASCII로만 구성되어 있는지 판단합니다.
use Illuminate\Support\Str;
$result = Str::of('Taylor')->isAscii();
// true
$result = Str::of('ü')->isAscii();
// false
isEmpty
isEmpty 메서드는 문자열이 비어있는지 확인합니다.
use Illuminate\Support\Str;
$result = Str::of(' ')->trim()->isEmpty();
// true
$result = Str::of('Laravel')->trim()->isEmpty();
// false
isNotEmpty
isNotEmpty 메서드는 문자열이 비어 있지 않은지 확인합니다.
use Illuminate\Support\Str;
$result = Str::of(' ')->trim()->isNotEmpty();
// false
$result = Str::of('Laravel')->trim()->isNotEmpty();
// true
isJson
isJson 메서드는 문자열이 유효한 JSON 형식인지 확인합니다.
use Illuminate\Support\Str;
$result = Str::of('[1,2,3]')->isJson();
// true
$result = Str::of('{"first": "John", "last": "Doe"}')->isJson();
// true
$result = Str::of('{first: "John", last: "Doe"}')->isJson();
// false
isUlid
isUlid 메서드는 문자열이 유효한 ULID인지 확인합니다.
use Illuminate\Support\Str;
$result = Str::of('01gd6r360bp37zj17nxb55yv40')->isUlid();
// true
$result = Str::of('Taylor')->isUlid();
// false
isUrl
isUrl 메서드는 문자열이 URL 형식인지 판단합니다.
use Illuminate\Support\Str;
$result = Str::of('http://example.com')->isUrl();
// true
$result = Str::of('Taylor')->isUrl();
// false
다양한 프로토콜이 기본적으로 허용됩니다. 확인할 프로토콜을 직접 지정할 수도 있습니다.
$result = Str::of('http://example.com')->isUrl(['http', 'https']);
isUuid
isUuid 메서드는 문자열이 유효한 UUID인지 확인합니다.
use Illuminate\Support\Str;
$result = Str::of('5ace9ab9-e9cf-4ec6-a19d-5881212a452c')->isUuid();
// true
$result = Str::of('Taylor')->isUuid();
// false
버전(1, 3, 4, 5, 6, 7, 8)별로 일치 여부를 확인할 수도 있습니다.
use Illuminate\Support\Str;
$isUuid = Str::of('a0a2a2d2-0b87-4a18-83f2-2529882be2de')->isUuid(version: 4);
// true
$isUuid = Str::of('a0a2a2d2-0b87-4a18-83f2-2529882be2de')->isUuid(version: 1);
// false
kebab
kebab 메서드는 문자열을 kebab-case로 변환합니다.
use Illuminate\Support\Str;
$converted = Str::of('fooBar')->kebab();
// foo-bar
lcfirst
lcfirst 메서드는 문자열의 첫 글자를 소문자로 변환합니다.
use Illuminate\Support\Str;
$string = Str::of('Foo Bar')->lcfirst();
// foo Bar
length
length 메서드는 문자열의 길이를 반환합니다.
use Illuminate\Support\Str;
$length = Str::of('Laravel')->length();
// 7
limit
limit 메서드는 문자열을 지정한 길이로 잘라냅니다.
use Illuminate\Support\Str;
$truncated = Str::of('The quick brown fox jumps over the lazy dog')->limit(20);
// The quick brown fox...
두 번째 인수로 잘린 뒤에 붙일 문자열을 지정할 수 있습니다.
$truncated = Str::of('The quick brown fox jumps over the lazy dog')->limit(20, ' (...)');
// The quick brown fox (...)
preserveWords 인수로 단어 경계까지만 자르도록 할 수 있습니다.
$truncated = Str::of('The quick brown fox')->limit(12, preserveWords: true);
// The quick...
lower
lower 메서드는 문자열을 모두 소문자로 변환합니다.
use Illuminate\Support\Str;
$result = Str::of('LARAVEL')->lower();
// 'laravel'
markdown
markdown 메서드는 GitHub 스타일의 마크다운을 HTML로 변환합니다.
use Illuminate\Support\Str;
$html = Str::of('# Laravel')->markdown();
// <h1>Laravel</h1>
$html = Str::of('# Taylor <b>Otwell</b>')->markdown([
'html_input' => 'strip',
]);
// <h1>Taylor Otwell</h1>
마크다운 보안
마크다운은 기본적으로 raw HTML을 지원하며, XSS 위험이 있습니다. CommonMark 보안 문서를 참고해 옵션(html_input, allow_unsafe_links)을 적절히 설정하세요.
use Illuminate\Support\Str;
Str::of('Inject: <script>alert("Hello XSS!");</script>')->markdown([
'html_input' => 'strip',
'allow_unsafe_links' => false,
]);
// <p>Inject: alert("Hello XSS!");</p>
mask
mask 메서드는 문자열의 일부를 반복 문자로 마스킹합니다. 이메일, 전화번호에서 부분 마스킹에 활용할 수 있습니다.
use Illuminate\Support\Str;
$string = Str::of('[email protected]')->mask('*', 3);
// tay***************
음수를 인덱스로 지정하거나, 마스킹 길이에도 음수를 지정할 수 있습니다.
$string = Str::of('[email protected]')->mask('*', -15, 3);
// tay***@example.com
$string = Str::of('[email protected]')->mask('*', 4, -4);
// tayl**********.com
match
match 메서드는 주어진 정규식 패턴과 일치하는 문자열 부분을 반환합니다.
use Illuminate\Support\Str;
$result = Str::of('foo bar')->match('/bar/');
// 'bar'
$result = Str::of('foo bar')->match('/foo (.*)/');
// 'bar'
matchAll
matchAll 메서드는 정규식에 일치하는 모든 부분을 컬렉션으로 반환합니다.
use Illuminate\Support\Str;
$result = Str::of('bar foo bar')->matchAll('/bar/');
// collect(['bar', 'bar'])
그룹 지정 시, 첫 번째 그룹의 값들만 반환합니다.
use Illuminate\Support\Str;
$result = Str::of('bar fun bar fly')->matchAll('/f(\w*)/');
// collect(['un', 'ly']);
매칭되는 값이 없으면 빈 컬렉션을 반환합니다.
isMatch
isMatch 메서드는 문자열이 주어진 정규식 패턴과 일치하는지 true/false를 반환합니다.
use Illuminate\Support\Str;
$result = Str::of('foo bar')->isMatch('/foo (.*)/');
// true
$result = Str::of('laravel')->isMatch('/foo (.*)/');
// false
newLine
newLine 메서드는 문자열 끝에 개행 문자(EOL)를 추가합니다.
use Illuminate\Support\Str;
$padded = Str::of('Laravel')->newLine()->append('Framework');
// 'Laravel
// Framework'
padBoth
padBoth 메서드는 문자열의 양쪽을 지정한 값으로 패딩하여 지정한 길이에 도달하게 합니다.
use Illuminate\Support\Str;
$padded = Str::of('James')->padBoth(10, '_');
// '__James___'
$padded = Str::of('James')->padBoth(10);
// ' James '
padLeft
padLeft 메서드는 문자열의 왼쪽을 지정한 값으로 패딩하여 지정 길이에 맞춥니다.
use Illuminate\Support\Str;
$padded = Str::of('James')->padLeft(10, '-=');
// '-=-=-James'
$padded = Str::of('James')->padLeft(10);
// ' James'
padRight
padRight 메서드는 문자열의 오른쪽을 지정한 값으로 패딩하여 일정 길이에 맞춥니다.
use Illuminate\Support\Str;
$padded = Str::of('James')->padRight(10, '-');
// 'James-----'
$padded = Str::of('James')->padRight(10);
// 'James '
pipe
pipe 메서드는 현 문자열 값을 지정한 콜러블(함수)에 넘겨 변환 결과를 반환합니다.
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
$hash = Str::of('Laravel')->pipe('md5')->prepend('Checksum: ');
// 'Checksum: a5c95b86291ea299fcbe64458ed12702'
$closure = Str::of('foo')->pipe(function (Stringable $str) {
return 'bar';
});
// 'bar'
plural
plural 메서드는 문자열을 복수형으로 변환합니다. Laravel의 다양한 복수화 언어를 지원합니다.
use Illuminate\Support\Str;
$plural = Str::of('car')->plural();
// cars
$plural = Str::of('child')->plural();
// children
인수로 숫자를 전달하면, 해당 숫자에 따라 단수/복수를 결정합니다.
use Illuminate\Support\Str;
$plural = Str::of('child')->plural(2);
// children
$plural = Str::of('child')->plural(1);
// child
prependCount 인수로 개수도 앞에 붙여줄 수 있습니다.
use Illuminate\Support\Str;
$label = Str::of('car')->plural(1000, prependCount: true);
// 1,000 cars
position
position 메서드는 문자열에서 지정한 서브스트링이 처음 나오는 위치를 반환합니다. 없으면 false가 반환됩니다.
use Illuminate\Support\Str;
$position = Str::of('Hello, World!')->position('Hello');
// 0
$position = Str::of('Hello, World!')->position('W');
// 7
prepend
prepend 메서드는 문자열 앞에 지정한 값을 붙입니다.
use Illuminate\Support\Str;
$string = Str::of('Framework')->prepend('Laravel ');
// Laravel Framework
remove
remove 메서드는 지정한 값(혹은 값 배열)을 문자열에서 제거합니다.
use Illuminate\Support\Str;
$string = Str::of('Arkansas is quite beautiful!')->remove('quite ');
// Arkansas is beautiful!
두 번째 파라미터로 false를 전달하면 대소문자 구분을 하지 않습니다.
repeat
repeat 메서드는 문자열을 지정된 횟수만큼 반복합니다.
use Illuminate\Support\Str;
$repeated = Str::of('a')->repeat(5);
// aaaaa
replace
replace 메서드는 문자열 내 지정한 값을 변경합니다.
use Illuminate\Support\Str;
$replaced = Str::of('Laravel 6.x')->replace('6.x', '7.x');
// Laravel 7.x
caseSensitive 인수로 대소문자 구분 여부를 선택할 수 있습니다.
$replaced = Str::of('macOS 13.x')->replace(
'macOS', 'iOS', caseSensitive: false
);
replaceArray
replaceArray 메서드는 배열의 값 순서대로 문자열에서 차례차례 값을 교체합니다.
use Illuminate\Support\Str;
$string = 'The event will take place between ? and ?';
$replaced = Str::of($string)->replaceArray('?', ['8:30', '9:00']);
// The event will take place between 8:30 and 9:00
replaceFirst
replaceFirst 메서드는 문자열에서 지정 값이 처음 등장한 부분만 대체합니다.
use Illuminate\Support\Str;
$replaced = Str::of('the quick brown fox jumps over the lazy dog')->replaceFirst('the', 'a');
// a quick brown fox jumps over the lazy dog
replaceLast
replaceLast 메서드는 문자열에서 지정 값이 마지막으로 등장한 부분만 대체합니다.
use Illuminate\Support\Str;
$replaced = Str::of('the quick brown fox jumps over the lazy dog')->replaceLast('the', 'a');
// the quick brown fox jumps over a lazy dog
replaceMatches
replaceMatches 메서드는 패턴에 맞는 문자열 부분을 모두 지정한 값으로 대체합니다.
use Illuminate\Support\Str;
$replaced = Str::of('(+1) 501-555-1000')->replaceMatches('/[^A-Za-z0-9]++/', '')
// '15015551000'
클로저로 복잡한 치환도 가능합니다.
use Illuminate\Support\Str;
$replaced = Str::of('123')->replaceMatches('/\d/', function (array $matches) {
return '['.$matches[0].']';
});
// '[1][2][3]'
replaceStart
replaceStart 메서드는 문자열이 지정 값으로 시작할 때에만 처음 등장하는 부분을 대체합니다.
use Illuminate\Support\Str;
$replaced = Str::of('Hello World')->replaceStart('Hello', 'Laravel');
// Laravel World
$replaced = Str::of('Hello World')->replaceStart('World', 'Laravel');
// Hello World
replaceEnd
replaceEnd 메서드는 문자열이 지정 값으로 끝날 때에만 마지막 등장하는 부분을 대체합니다.
use Illuminate\Support\Str;
$replaced = Str::of('Hello World')->replaceEnd('World', 'Laravel');
// Hello Laravel
$replaced = Str::of('Hello World')->replaceEnd('Hello', 'Laravel');
// Hello World
scan
scan 메서드는 sscanf PHP 함수가 지원하는 형식에 따라 입력 문자열을 파싱해 컬렉션으로 반환합니다.
use Illuminate\Support\Str;
$collection = Str::of('filename.jpg')->scan('%[^.].%s');
// collect(['filename', 'jpg'])
singular
singular 메서드는 문자열을 단수형으로 변환합니다. Laravel의 다양한 복수화 언어를 지원합니다.
use Illuminate\Support\Str;
$singular = Str::of('cars')->singular();
// car
$singular = Str::of('children')->singular();
// child
slug
slug 메서드는 문자열을 URL에서 사용하기 좋은 "슬러그(slug)" 형태로 변환합니다.
use Illuminate\Support\Str;
$slug = Str::of('Laravel Framework')->slug('-');
// laravel-framework
snake
snake 메서드는 문자열을 snake_case로 변환합니다.
use Illuminate\Support\Str;
$converted = Str::of('fooBar')->snake();
// foo_bar
split
split 메서드는 정규식을 사용해 문자열을 컬렉션으로 분리합니다.
use Illuminate\Support\Str;
$segments = Str::of('one, two, three')->split('/[\s,]+/');
// collect(["one", "two", "three"])
squish
squish 메서드는 문자열의 앞뒤 및 중간의 불필요한 공백을 모두 제거합니다.
use Illuminate\Support\Str;
$string = Str::of(' laravel framework ')->squish();
// laravel framework
start
start 메서드는 문자열이 지정 값으로 시작하지 않으면, 앞쪽에 단 한 번만 그 값을 붙입니다.
use Illuminate\Support\Str;
$adjusted = Str::of('this/string')->start('/');
// /this/string
$adjusted = Str::of('/this/string')->start('/');
// /this/string
startsWith
startsWith 메서드는 문자열이 지정 값으로 시작하는지 확인합니다.
use Illuminate\Support\Str;
$result = Str::of('This is my name')->startsWith('This');
// true
배열을 전달하면, 값 중 하나라도 일치하면 true를 반환합니다.
use Illuminate\Support\Str;
$result = Str::of('This is my name')->startsWith(['This', 'That']);
// true
stripTags
stripTags 메서드는 문자열의 HTML 태그 및 PHP 태그를 제거합니다.
use Illuminate\Support\Str;
$result = Str::of('<a href="https://laravel.com">Taylor <b>Otwell</b></a>')->stripTags();
// Taylor Otwell
$result = Str::of('<a href="https://laravel.com">Taylor <b>Otwell</b></a>')->stripTags('<b>');
// Taylor <b>Otwell</b>
studly
studly 메서드는 문자열을 StudlyCase로 변환합니다.
use Illuminate\Support\Str;
$converted = Str::of('foo_bar')->studly();
// FooBar
substr
substr 메서드는 시작 위치와 길이 값을 기준으로 부분 문자열을 반환합니다.
use Illuminate\Support\Str;
$string = Str::of('Laravel Framework')->substr(8);
// Framework
$string = Str::of('Laravel Framework')->substr(8, 5);
// Frame
substrReplace
substrReplace 메서드는 문자열의 지정 위치에서 지정 길이만큼을 대체하거나, 길이에 0을 주면 문자열을 삽입합니다.
use Illuminate\Support\Str;
$string = Str::of('1300')->substrReplace(':', 2);
// 13:
$string = Str::of('The Framework')->substrReplace(' Laravel', 3, 0);
// The Laravel Framework
swap
swap 메서드는 PHP의 strtr 함수를 활용해 여러 값을 한 번에 교체합니다.
use Illuminate\Support\Str;
$string = Str::of('Tacos are great!')
->swap([
'Tacos' => 'Burritos',
'great' => 'fantastic',
]);
// Burritos are fantastic!
take
take 메서드는 문자열의 앞에서부터 지정한 만큼의 문자를 반환합니다.
use Illuminate\Support\Str;
$taken = Str::of('Build something amazing!')->take(5);
// Build
tap
tap 메서드는 현재 문자열을 클로저에 전달해 살펴볼 수 있게 하며, 클로저의 반환값과 무관하게 원래 문자열이 반환됩니다.
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
$string = Str::of('Laravel')
->append(' Framework')
->tap(function (Stringable $string) {
dump('String after append: '.$string);
})
->upper();
// LARAVEL FRAMEWORK
test
test 메서드는 문자열이 지정한 정규식과 일치하는지 확인합니다.
use Illuminate\Support\Str;
$result = Str::of('Laravel Framework')->test('/Laravel/');
// true
title
title 메서드는 문자열을 각 단어의 첫 글자가 대문자인 형태로 변환합니다.
use Illuminate\Support\Str;
$converted = Str::of('a nice title uses the correct case')->title();
// A Nice Title Uses The Correct Case
toBase64
toBase64 메서드는 문자열을 Base64로 변환합니다.
use Illuminate\Support\Str;
$base64 = Str::of('Laravel')->toBase64();
// TGFyYXZlbA==
toHtmlString
toHtmlString 메서드는 문자열을 Illuminate\Support\HtmlString 인스턴스로 변환해 Blade 템플릿에서 escape 처리 없이 렌더링할 수 있게 합니다.
use Illuminate\Support\Str;
$htmlString = Str::of('Nuno Maduro')->toHtmlString();
toUri
toUri 메서드는 문자열을 Illuminate\Support\Uri 인스턴스로 변환합니다.
use Illuminate\Support\Str;
$uri = Str::of('https://example.com')->toUri();
transliterate
transliterate 메서드는 가능한 한 유사한 ASCII 문자로 변환을 시도합니다.
use Illuminate\Support\Str;
$email = Str::of('ⓣⓔⓢⓣ@ⓛⓐⓡⓐⓥⓔⓛ.ⓒⓞⓜ')->transliterate()
// '[email protected]'
trim
trim 메서드는 문자열의 앞뒤 공백이나 지정 문자를 제거합니다. PHP의 기본 trim 함수와 달리 Unicode 공백 문자도 제거합니다.
use Illuminate\Support\Str;
$string = Str::of(' Laravel ')->trim();
// 'Laravel'
$string = Str::of('/Laravel/')->trim('/');
// 'Laravel'
ltrim
ltrim 메서드는 문자열의 왼쪽 부분의 공백 혹은 지정 문자를 제거합니다. PHP의 기본 ltrim 함수와 달리 Unicode 공백 문자도 제거합니다.
use Illuminate\Support\Str;
$string = Str::of(' Laravel ')->ltrim();
// 'Laravel '
$string = Str::of('/Laravel/')->ltrim('/');
// 'Laravel/'
rtrim
rtrim 메서드는 문자열의 오른쪽 부분의 공백 혹은 지정 문자를 제거합니다. PHP의 기본 rtrim 함수와 달리 Unicode 공백 문자도 제거합니다.
use Illuminate\Support\Str;
$string = Str::of(' Laravel ')->rtrim();
// ' Laravel'
$string = Str::of('/Laravel/')->rtrim('/');
// '/Laravel'
ucfirst
ucfirst 메서드는 문자열의 첫 글자를 대문자로 반환합니다.
use Illuminate\Support\Str;
$string = Str::of('foo bar')->ucfirst();
// Foo bar
ucsplit
ucsplit 메서드는 대문자를 기준으로 문자열을 컬렉션에 나눕니다.
use Illuminate\Support\Str;
$string = Str::of('Foo Bar')->ucsplit();
// collect(['Foo ', 'Bar'])
ucwords
ucwords 메서드는 문자열의 각 단어의 첫 글자를 대문자로 변환합니다.
use Illuminate\Support\Str;
$string = Str::of('laravel framework')->ucwords();
// Laravel Framework
unwrap
unwrap 메서드는 주어진 문자열 앞뒤에서 지정한 값을 제거합니다.
use Illuminate\Support\Str;
Str::of('-Laravel-')->unwrap('-');
// Laravel
Str::of('{framework: "Laravel"}')->unwrap('{', '}');
// framework: "Laravel"
upper
upper 메서드는 문자열을 모두 대문자로 변환합니다.
use Illuminate\Support\Str;
$adjusted = Str::of('laravel')->upper();
// LARAVEL
when
when 메서드는 주어진 조건이 true일 때 지정한 클로저를 실행합니다. 클로저는 플루언트 문자열 인스턴스를 인자로 받습니다.
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
$string = Str::of('Taylor')
->when(true, function (Stringable $string) {
return $string->append(' Otwell');
});
// 'Taylor Otwell'
필요하다면, 세 번째 인자로 조건이 false 일 때 실행할 클로저도 전달할 수 있습니다.
whenContains
whenContains 메서드는 문자열이 특정 값을 포함하면 지정 클로저를 실행합니다. 클로저는 플루언트 문자열 인스턴스를 전달받습니다.
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
$string = Str::of('tony stark')
->whenContains('tony', function (Stringable $string) {
return $string->title();
});
// 'Tony Stark'
세 번째 인자로 부정 조건일 때 실행할 클로저도 전달할 수 있습니다.
여러 값을 배열로 넘기면, 배열 내 하나라도 포함되어 있을 때 클로저가 실행됩니다.
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
$string = Str::of('tony stark')
->whenContains(['tony', 'hulk'], function (Stringable $string) {
return $string->title();
});
// Tony Stark
whenContainsAll
whenContainsAll 메서드는 문자열이 모든 서브스트링을 포함할 때 클로저를 실행합니다.
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
$string = Str::of('tony stark')
->whenContainsAll(['tony', 'stark'], function (Stringable $string) {
return $string->title();
});
// 'Tony Stark'
세 번째 인자로 false 조건일 때 실행할 클로저도 전달할 수 있습니다.
whenDoesntEndWith
whenDoesntEndWith 메서드는 문자열이 서브스트링으로 끝나지 않을 때 클로저를 실행합니다.
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
$string = Str::of('disney world')->whenDoesntEndWith('land', function (Stringable $string) {
return $string->title();
});
// 'Disney World'
whenDoesntStartWith
whenDoesntStartWith 메서드는 문자열이 서브스트링으로 시작하지 않을 때 클로저를 실행합니다.
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
$string = Str::of('disney world')->whenDoesntStartWith('sea', function (Stringable $string) {
return $string->title();
});
// 'Disney World'
whenEmpty
whenEmpty 메서드는 문자열이 비어 있을 때 클로저를 실행합니다. 클로저가 값을 반환하면 그 값이 반환되며, 그렇지 않으면 원래 플루언트 문자열 자체가 반환됩니다.
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
$string = Str::of(' ')->trim()->whenEmpty(function (Stringable $string) {
return $string->prepend('Laravel');
});
// 'Laravel'
whenNotEmpty
whenNotEmpty 메서드는 문자열이 비어 있지 않을 때 클로저를 실행합니다. 반환 값 규칙은 whenEmpty와 동일합니다.
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
$string = Str::of('Framework')->whenNotEmpty(function (Stringable $string) {
return $string->prepend('Laravel ');
});
// 'Laravel Framework'
whenStartsWith
whenStartsWith 메서드는 문자열이 서브스트링으로 시작하면 클로저를 실행합니다.
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
$string = Str::of('disney world')->whenStartsWith('disney', function (Stringable $string) {
return $string->title();
});
// 'Disney World'
whenEndsWith
whenEndsWith 메서드는 문자열이 서브스트링으로 끝나면 클로저를 실행합니다.
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
$string = Str::of('disney world')->whenEndsWith('world', function (Stringable $string) {
return $string->title();
});
// 'Disney World'
whenExactly
whenExactly 메서드는 문자열이 주어진 문자열과 정확히 일치하면 클로저를 실행합니다.
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
$string = Str::of('laravel')->whenExactly('laravel', function (Stringable $string) {
return $string->title();
});
// 'Laravel'
whenNotExactly
whenNotExactly 메서드는 문자열이 지정한 문자열과 정확히 일치하지 않을 때 클로저를 실행합니다.
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
$string = Str::of('framework')->whenNotExactly('laravel', function (Stringable $string) {
return $string->title();
});
// 'Framework'
whenIs
whenIs 메서드는 문자열이 지정한 패턴과 일치하면 클로저를 실행합니다. 별표(*) 와일드카드 사용 가능. 클로저엔 플루언트 인스턴스가 전달됩니다.
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
$string = Str::of('foo/bar')->whenIs('foo/*', function (Stringable $string) {
return $string->append('/baz');
});
// 'foo/bar/baz'
whenIsAscii
whenIsAscii 메서드는 문자열이 7비트 ASCII면 클로저를 실행합니다.
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
$string = Str::of('laravel')->whenIsAscii(function (Stringable $string) {
return $string->title();
});
// 'Laravel'
whenIsUlid
whenIsUlid 메서드는 문자열이 유효한 ULID면 클로저를 실행합니다.
use Illuminate\Support\Str;
$string = Str::of('01gd6r360bp37zj17nxb55yv40')->whenIsUlid(function (Stringable $string) {
return $string->substr(0, 8);
});
// '01gd6r36'
whenIsUuid
whenIsUuid 메서드는 문자열이 유효한 UUID면 클로저를 실행합니다.
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
$string = Str::of('a0a2a2d2-0b87-4a18-83f2-2529882be2de')->whenIsUuid(function (Stringable $string) {
return $string->substr(0, 8);
});
// 'a0a2a2d2'
whenTest
whenTest 메서드는 문자열이 지정한 정규식에 맞으면 클로저를 실행합니다.
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
$string = Str::of('laravel framework')->whenTest('/laravel/', function (Stringable $string) {
return $string->title();
});
// 'Laravel Framework'
wordCount
wordCount 메서드는 문자열 내 단어 개수를 반환합니다.
use Illuminate\Support\Str;
Str::of('Hello, world!')->wordCount(); // 2
words
words 메서드는 문자열에서 지정된 개수 만큼만 단어를 남기고, 잘린 경우 뒤에 표시할 문자열을 설정할 수 있습니다.
use Illuminate\Support\Str;
$string = Str::of('Perfectly balanced, as all things should be.')->words(3, ' >>>');
// Perfectly balanced, as >>>
wrap
wrap 메서드는 문자열의 앞뒤에 또 다른 문자열(또는 쌍)을 감쌉니다.
use Illuminate\Support\Str;
Str::of('Laravel')->wrap('"');
// "Laravel"
Str::is('is')->wrap(before: 'This ', after: ' Laravel!');
// This is Laravel!