
preg_replace 함수의 패턴은 정규 표현식을 사용하여 작성합니다.
예를 들어, 'hello world' 문자열에서 'world'를 'earth'로 바꾸고 싶다면, 다음과 같이 패턴과 대체 문자열을 설정할 수 있습니다.
#hostingforum.kr
php
$string = 'hello world';
$pattern = '/world/';
$replacement = 'earth';
$result = preg_replace($pattern, $replacement, $string);
print($result); // 출력: hello earth
위 예제에서 '/world/'는 'world'를 찾는 패턴입니다. '/'는 정규 표현식에서 패턴을 시작하고 끝내는 표시입니다.
대체 문자열 'earth'는 'world'를 찾은 경우에 사용됩니다.
위 예제에서 $result는 'hello earth'를 출력합니다.
이러한 패턴과 대체 문자열을 사용하여, 'hello world' 문자열에서 'world'를 'earth'로 바꾸는 작업을 수행할 수 있습니다.
2025-05-31 02:23