PHP preg_split() 함수
예시
preg_split()을 사용하여 날짜를 구성 요소로 분할합니다.
<?php
$date = "1970-01-01 00:00:00";
$pattern = "/[-\s:]/";
$components =
preg_split($pattern, $date);
print_r($components);
?>
정의 및 사용
이 preg_split()
함수는 구분 기호로 정규 표현식의 일치 항목을 사용하여 문자열을 배열로 나눕니다.
통사론
preg_split(pattern, string, limit, flags)
매개변수 값
Parameter | Description |
---|---|
pattern | Required. A regular expression determining what to use as a separator |
string | Required. The string that is being split |
limit | Optional. Defaults to -1, meaning unlimited. Limits the number of elements that the returned array can have. If the limit is reached before all of the separators have been found, the rest of the string will be put into the last element of the array |
flags | Optional. These flags provide options to change the returned array:
|
기술적 세부 사항
반환 값: | 각 항목이 정규식의 일치 항목으로 구분된 입력 문자열의 일부에 해당하는 부분 문자열 배열을 반환합니다. |
---|---|
PHP 버전: | 4+ |
더 많은 예
예시
PREG_SPLIT_DELIM_CAPTURE 플래그 사용:
<?php
$date = "1970-01-01 00:00:00";
$pattern = "/([-\s:])/";
$components =
preg_split($pattern, $date, -1,
PREG_SPLIT_DELIM_CAPTURE);
print_r($components);
?>
예시
PREG_SPLIT_OFFSET_CAPTURE 플래그 사용:
<?php
$date = "1970-01-01";
$pattern = "/-/";
$components =
preg_split($pattern, $date, -1,
PREG_SPLIT_OFFSET_CAPTURE);
print_r($components);
?>
❮ PHP RegExp 참조