PHP substr_replace() 함수
예시
"Hello"를 "world"로 바꿉니다.
<?php
echo substr_replace("Hello","world",0);
?>
정의 및 사용
substr_replace() 함수는 문자열의 일부를 다른 문자열로 바꿉니다.
참고: 시작 매개변수가 음수이고 길이가 시작보다 작거나 같으면 길이는 0이 됩니다.
참고: 이 함수는 바이너리 안전합니다.
통사론
substr_replace(string,replacement,start,length)
매개변수 값
Parameter | Description |
---|---|
string | Required. Specifies the string to check |
replacement | Required. Specifies the string to insert |
start | Required. Specifies where to start replacing in the string
|
length | Optional. Specifies how many characters should be replaced. Default is the same length as the string.
|
기술적 세부 사항
반환 값: | 교체된 문자열을 반환합니다. 문자열이 배열이면 배열이 반환됩니다. |
---|---|
PHP 버전: | 4+ |
변경 로그: | PHP 4.3.3부터 모든 매개변수는 이제 배열을 허용합니다. |
더 많은 예
예시
문자열의 6번째 위치에서 바꾸기 시작("world"를 "earth"로 바꾸기):
<?php
echo substr_replace("Hello world","earth",6);
?>
예시
문자열 끝에서 5번째 위치에서 바꾸기 시작("world"를 "earth"로 바꾸기):
<?php
echo substr_replace("Hello world","earth",-5);
?>
예시
"world"의 시작 부분에 "Hello"를 삽입합니다.
<?php
echo substr_replace("world","Hello ",0,0);
?>
예시
한 번에 여러 문자열을 바꿉니다. 각 문자열의 "AAA"를 "BBB"로 바꿉니다.
<?php
$replace = array("1: AAA","2: AAA","3: AAA");
echo implode("<br>",substr_replace($replace,'BBB',3,3));
?>
❮ PHP 문자열 참조