Open In App

PHP | preg_split() Function

Last Updated : 11 May, 2022
Improve
Improve
Like Article
Like
Save
Share
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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads