Funkce PHP preg_split().
❮ Referenční příručka PHP RegExp
Příklad
Použijte preg_split() k rozdělení data na jeho součásti:
<?php
$date = "1970-01-01 00:00:00";
$pattern = "/[-\s:]/";
$components =
preg_split($pattern, $date);
print_r($components);
?>
Definice a použití
Funkce preg_split()
rozloží řetězec do pole pomocí shod regulárního výrazu jako oddělovačů.
Syntax
preg_split(pattern, string, limit, flags)
Hodnoty parametrů
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:
|
Technické údaje
Návratová hodnota: | Vrátí pole podřetězců, kde každá položka odpovídá části vstupního řetězce oddělené shodou regulárního výrazu |
---|---|
Verze PHP: | 4+ |
Další příklady
Příklad
Použití příznaku 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);
?>
Příklad
Použití příznaku PREG_SPLIT_OFFSET_CAPTURE:
<?php
$date = "1970-01-01";
$pattern = "/-/";
$components =
preg_split($pattern, $date, -1,
PREG_SPLIT_OFFSET_CAPTURE);
print_r($components);
?>
❮ Referenční příručka PHP RegExp