Current Location: Home> Function Categories> preg_split

preg_split

Separate strings by a regular expression
Name:preg_split
Category:Regular processing PCRE
Programming Language:php
One-line Description:Use the match of the regular expression as the separator to split the string into an array.

Definition and usage

preg_split() function uses the match of the regular expression as a delimiter to split the string into an array.

Example

Example 1

Use preg_split() to split the date into its components:

 <?php
$date = "1970-01-01 00:00:00" ;
$pattern = "/[-\s:]/" ;
$components = preg_split ( $pattern , $date ) ;
print_r ( $components ) ;
?>

Try it yourself

Example 2

Use the PREG_SPLIT_DELIM_CAPTURE flag:

 <?php
$date = "1970-01-01 00:00:00" ;
$pattern = "/([-\s:])/" ;
$components = preg_split ( $pattern , $date , - 1 ,
PREG_SPLIT_DELIM_CAPTURE ) ;
print_r ( $components ) ;
?>

Try it yourself

Example 3

Use the PREG_SPLIT_OFFSET_CAPTURE flag:

 <?php
$date = "1970-01-01" ;
$pattern = "/-/" ;
$components = preg_split ( $pattern , $date , - 1 ,
PREG_SPLIT_OFFSET_CAPTURE ) ;
print_r ( $components ) ;
?>

Try it yourself

Similar Functions
Popular Articles