PHP str_ireplace() 함수
예시
"Hello world!" 문자열에서 "WORLD"(대소문자 구분하지 않음) 문자를 교체합니다. "피터"와 함께:
<?php
echo str_ireplace("WORLD","Peter","Hello world!");
?>
정의 및 사용
str_ireplace() 함수는 문자열의 일부 문자를 다른 문자로 바꿉니다.
이 기능은 다음 규칙에 따라 작동합니다.
- 검색할 문자열이 배열이면 배열을 반환합니다.
- 검색할 문자열이 배열이면 모든 배열 요소에 대해 찾기 및 바꾸기를 수행합니다.
- 찾기와 바꾸기가 모두 배열이고 바꾸기가 찾기보다 요소 수가 적은 경우 빈 문자열이 바꾸기로 사용됩니다.
- find가 배열이고 replace가 문자열이면 모든 찾기 값에 대해 대체 문자열이 사용됩니다.
참고: 이 함수는 대소문자를 구분하지 않습니다. str_replace() 함수를 사용하여 대소문자를 구분하는 검색을 수행하십시오 .
참고: 이 함수는 바이너리 안전합니다.
통사론
str_ireplace(find,replace,string,count)
매개변수 값
Parameter | Description |
---|---|
find | Required. Specifies the value to find |
replace | Required. Specifies the value to replace the value in find |
string | Required. Specifies the string to be searched |
count | Optional. A variable that counts the number of replacements |
기술적 세부 사항
반환 값: | 교체된 값이 있는 문자열 또는 배열을 반환합니다. |
---|---|
PHP 버전: | 5+ |
변경 로그: | count 매개변수는 PHP 5.0에 추가되었습니다 . |
더 많은 예
예시
배열 및 카운트 변수와 함께 str_ireplace() 사용:
<?php
$arr = array("blue","red","green","yellow");
print_r(str_ireplace("RED","pink",$arr,$i)); // This function is case-insensitive
echo "Replacements: $i";
?>
예시
찾기보다 교체에 더 적은 요소가 있는 str_ireplace() 사용:
<?php
$find = array("HELLO","WORLD");
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_ireplace($find,$replace,$arr));
?>
❮ PHP 문자열 참조