Multibyte mb_ereg_replace_callback
(PHP 5 >= 5.4.1, PHP 7, PHP 8)
mb_ereg_replace_callback — 정규식 검색을 수행하고 콜백을 사용하여 멀티바이트 지원으로 대체
설명
mb_ereg_replace_callback( string $pattern, callable $callback, string $string, ?string $options = null ): string|false|null
pattern
과 일치하는 string
을 검색한 다음 일치하는 텍스트를 callback
함수의 출력으로 바꿉니다.
이 함수의 동작은 replacement
매개변수 대신 callback
을 지정해야 한다는 사실을 제외하고는 mb_ereg_replace()와 거의 동일합니다.
매개변수
pattern
- 정규식 패턴.
code>pattern에서 멀티바이트 문자를 사용할 수 있습니다.
callback
subject
문자열에서 일치하는 요소의 배열을 호출하고 전달하는 콜백입니다. 콜백은 대체 문자열을 반환해야 합니다.종종 한 곳에서 mb_ereg_replace_callback()에 대한
callback
함수가 필요합니다. 이 경우 익명 함수를 사용하여 mb_ereg_replace_callback() 호출 내에서 콜백을 선언할 수 있습니다. 이렇게 하면 호출에 대한 모든 정보를 한 곳에서 볼 수 있고 다른 곳에서는 사용되지 않은 콜백 함수 이름으로 함수 네임스페이스를 어지럽히지 않습니다.string
- 검사 중인 문자열입니다.
options
- 검색 옵션입니다. 설명은 mb_regex_set_options()를 참조하십시오.
반환 값
성공 시 결과 문자열, 오류 시 false
. 문자열이 현재 인코딩에 유효하지 않으면 null<
이 반환됩니다.
변경 로그
Version | Description |
---|---|
8.0.0 | options 은 이제 nullable입니다. |
7.1.0 | 이 함수는 string 이 현재 인코딩에 유효한지 여부를 확인합니다. |
Examples
예제 #1 mb_ereg_replace_callback() 예제
<?php
// this text was used in 2002
// we want to get this up to date for 2003
$text = "April fools day is 04/01/2002\n";
$text.= "Last christmas was 12/24/2001\n";
// the callback function
function next_year($matches)
{
// as usual: $matches[0] is the complete match
// $matches[1] the match for the first subpattern
// enclosed in '(...)' and so on
return $matches[1].($matches[2]+1);
}
echo mb_ereg_replace_callback(
"(\d{2}/\d{2}/)(\d{4})",
"next_year",
$text);
?>
위의 예는 다음을 출력합니다.
April fools day is 04/01/2003 Last christmas was 12/24/2002
예제 #2 익명 함수를 사용하는 mb_ereg_replace_callback()
<?php
// this text was used in 2002
// we want to get this up to date for 2003
$text = "April fools day is 04/01/2002\n";
$text.= "Last christmas was 12/24/2001\n";
echo mb_ereg_replace_callback(
"(\d{2}/\d{2}/)(\d{4})",
function ($matches) {
return $matches[1].($matches[2]+1);
},
$text);
?>
메모
메모: 내부 인코딩 또는 mb_regex_encoding()에 의해 지정된 문자 인코딩이 이 함수의 문자 인코딩으로 사용됩니다.
기타
- mb_regex_encoding() - 멀티바이트 정규식에 대한 문자 인코딩 설정/가져오기
- mb_ereg_replace() - 정규식을 멀티바이트 지원으로 교체
- Anonymous functions