본문 바로가기

문자열

번역일: 2026년 6월 20일

문자열

소개

Laravel은 문자열 값을 다루기 위한 다양한 함수를 제공합니다. 이 중 많은 함수는 프레임워크 내부에서 사용되지만, 필요하다면 여러분의 애플리케이션에서도 자유롭게 활용할 수 있습니다.

사용 가능한 메서드

문자열 함수

유창한 문자열 (Fluent Strings)

문자열 함수

`__()` {.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>'); // &lt;html&gt;foo&lt;/html&gt;

`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']); // 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', 'foo']); // 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::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 메서드는 CommonMark를 사용하여 GitHub 스타일 마크다운을 인라인 HTML로 변환합니다. markdown 메서드와 달리 생성된 HTML 전체를 블록 수준 요소로 감싸지 않습니다.

use Illuminate\Support\Str; $html = Str::inlineMarkdown('**Laravel**'); // <strong>Laravel</strong>

마크다운 보안

마크다운은 기본적으로 원시 HTML을 허용하므로, 사용자 입력을 그대로 사용하면 XSS(크로스사이트 스크립팅) 취약점이 발생할 수 있습니다. CommonMark 보안 문서에 따라, html_input 옵션으로 원시 HTML을 이스케이프하거나 제거할 수 있으며, allow_unsafe_links 옵션으로 안전하지 않은 링크 허용 여부를 지정할 수 있습니다. 일부 원시 HTML을 허용해야 한다면 컴파일된 마크다운을 HTML Purifier를 통해 처리하세요.

use Illuminate\Support\Str; Str::inlineMarkdown('삽입 시도: <script>alert("Hello XSS!");</script>', [ 'html_input' => 'strip', 'allow_unsafe_links' => false, ]); // 삽입 시도: alert(&quot;Hello XSS!&quot;);

`Str::is()` {.collection-method}

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()` {.collection-method}

Str::isAscii 메서드는 주어진 문자열이 7비트 ASCII인지 확인합니다.

use

이 문서는 Laravel 공식 문서(MIT)를 한국 개발자를 위해 번역·재구성한 것입니다.

번역일: 2026년 6월 20일