PHP preg_split() İşlevi
Örnek
Bir tarihi bileşenlerine ayırmak için preg_split() kullanın:
<?php
$date = "1970-01-01 00:00:00";
$pattern = "/[-\s:]/";
$components =
preg_split($pattern, $date);
print_r($components);
?>
Tanım ve Kullanım
İşlev preg_split()
, ayırıcı olarak normal bir ifadenin eşleşmelerini kullanarak bir dizeyi diziye ayırır.
Sözdizimi
preg_split(pattern, string, limit, flags)
Parametre Değerleri
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:
|
Teknik detaylar
Geri dönüş değeri: | Her öğenin, normal ifadenin eşleşmesiyle ayrılmış giriş dizesinin bir bölümüne karşılık geldiği bir alt dize dizisi döndürür |
---|---|
PHP Sürümü: | 4+ |
Daha fazla örnek
Örnek
PREG_SPLIT_DELIM_CAPTURE bayrağını kullanarak:
<?php
$date = "1970-01-01 00:00:00";
$pattern = "/([-\s:])/";
$components =
preg_split($pattern, $date, -1,
PREG_SPLIT_DELIM_CAPTURE);
print_r($components);
?>
Örnek
PREG_SPLIT_OFFSET_CAPTURE bayrağını kullanarak:
<?php
$date = "1970-01-01";
$pattern = "/-/";
$components =
preg_split($pattern, $date, -1,
PREG_SPLIT_OFFSET_CAPTURE);
print_r($components);
?>
❮ PHP RegExp Referansı