PHP preg_split() Function

PHP preg_quote() Function
PHP preg_replace_callback_array() Function

In this article, you will learn how to split a string based on some pattern in PHP. The preg_split() function in PHP breaks a string into an array using matches of a regular expression as separators.

what is the syntax of the PREG_SPLIT function in php?

preg_split(pattern, string, limit, flags)
patternRequired. A regular expression determining what to use as a separator
stringRequired. The string that is being split
limitOptional. 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
flagsOptional. These flags provide options to change the returned array:
PREG_SPLIT_NO_EMPTY – Empty strings will be removed from the returned array.PREG_SPLIT_DELIM_CAPTURE – If the regular expression contains a group wrapped in parentheses, matches of this group will be included in the returned array.PREG_SPLIT_OFFSET_CAPTURE – Each element in the returned array will be an array with two element, where the first element is the substring and the second element is the position of the first character of the substring in the input string.

examples of the PREG_SPLIT function

Example 1. In this example, we use preg_split() to split a date into its components.

$date = "1970-01-01 00:00:00";
$pattern = "/[-\s:]/";
$components = preg_split($pattern, $date);
PHP preg_quote() Function
PHP preg_replace_callback_array() Function
en English
Scroll to Top