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);
?>
❮PHPRegExpリファレンス