PHP | preg_split() Function Last Updated : 11 May, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report The preg_split() function is an inbuilt function in PHP which is used to convert the given string into an array. The function splits the string into smaller strings or sub-strings of length which is specified by the user. If the limit is specified then small string or sub-strings up to limit return through an array. The preg_split() function is similar to explode() function but the difference is used to the regular expression to specify the delimiter but explode is not used it. Syntax: array preg_split( $pattern, $subject, $limit, $flag ) Parameter: This function accepts four parameters as mentioned above and described below: $pattern: The value is string type which the pattern to search as a string otherwise its separates the elements.$subject: The $subject is variable which is used to store input string.$limit: The $limit is indicates the limit. If the limit is specified ,then small or sub-string to be returned up to limit.If limit is 0 or -1 ,it indicates "no limit" then used by flag ($strflag).$flags: The $flags is used for signalize and its variable type used for indicates two state True or False to control the program. Its combinations of different flags such as below:PREG_SPLIT_NO_EMPTY: If flag variable is set to PREG_SPLIT_NO_EMPTY, then only non-empty pieces will be returned by preg_split() function.PREG_SPLIT_DELIM_CAPTURE: If flag variable is set to PREG_SPLIT_DELIM_CAPTURE, the parenthesized expression in the delimiter pattern will be captured and returned as well.PREG_SPLIT_OFFSET_CAPTURE: If flag variable is set to PREG_SPLIT_OFFSET_CAPTURE, for each occurring match the appendant string offset will be returned and changes the return value in an array where matched string offset will be 0 and input string offset will be 1. Return Value: This function returns an array after the split boundaries matched. When the limit of the original array or string exceeds then returns with an array element otherwise it's False. Below programs illustrate the preg_split() function in PHP: Program 1: php <?php // Input string $inputstrVal = 'Geeksarticle'; // Implementation of preg_split() function $result = preg_split('//', $inputstrVal , -1, PREG_SPLIT_NO_EMPTY); // Display result print_r($result); ?> Output:Array ( [0] => G [1] => e [2] => e [3] => k [4] => s [5] => a [6] => r [7] => t [8] => i [9] => c [10] => l [11] => e ) Program 2: php <?php // PHP program of preg_split() function // split the phrase by any number of commas // space characters include \r, \t, \n and \f $result = preg_split("/[\s,]+/", "Geeks for Geeks"); // Display result print_r($result); ?> Output:Array ( [0] => Geeks [1] => for [2] => Geeks ) Program 3: php <?php // PHP program to implementation of // preg_split() function // Input original string $inputstrVal = "http://php.net/archive/2018.php"; $patternstrVal= "/[http:\/\/|\.]/"; // Implement preg_split() function $result = preg_split($patternstrVal, $inputstrVal, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE); // Display result print_r($result ); ?> Output:Array ( [0] => Array ( [0] => ne [1] => 11 ) [1] => Array ( [0] => arc [1] => 15 ) [2] => Array ( [0] => ive [1] => 19 ) [3] => Array ( [0] => 2018 [1] => 23 ) ) Reference: http://php.net/manual/en/function.preg-split.php Comment More infoAdvertise with us Next Article PHP | preg_grep() Function J jit_t Follow Improve Article Tags : Web Technologies PHP PHP-function Similar Reads PHP | preg_replace() Function The preg_replace() function is an inbuilt function in PHP that is used to perform a regular expression for search and replace the content. Syntax: preg_replace( $pattern, $replacement, $subject, $limit, $count ) Parameters: This function accepts five parameters as mentioned above and described below 2 min read PHP mb_split() Function The mb_split() is an inbuilt PHP function that split the multibyte strings with help of the regular expression. Syntax: mb_split(pattern,string,limit): array|falseParameters: Â This function accepts 3 parameters: pattern: In this parameter, we define a regular expression that helps us to split a stri 1 min read PHP | preg_grep() Function The preg_grep() is an inbuilt function in PHP. It returns the array consisting of the elements of the input array that match with the given pattern. Syntax : array preg_grep ( $pattern, $input [, $flags] ) Parameters Used: The preg_grep() function take three parameters that are described below: $pat 2 min read Node.js split() Function Splitting a string is one of the most widely used functions by the split() method, which splits a string into an array of substrings. This function belongs to the prototype in JavaScript and is applied in many kinds of activities like data parsing from files, extracting information from URLs, or tex 2 min read PHP strstr() Function The strstr() function is a built-in function in PHP. It searches for the first occurrence of a string inside another string and displays the portion of the latter starting from the first occurrence of the former in the latter (before if specified). This function is case-sensitive. Syntax : strstr( $ 2 min read PHP mb_str_split() Function The mb_str_split() function was introduced in the release of PHP version 7.4.0 and it is only supported with PHP versions equal to or greater than 7.4.0. The mb_str_split() function serves as an alternate of str_split() function. It is used to split the given string with the specified length of chun 2 min read Like