Change language

PHP Preg_split () function

| |
Syntax:
array preg_split ($pattern, $subject, $limit, $flag) 
Parameter:this function takes four parameters as above and described below:
  • $pattern:the value is a string type, which is the pattern to search for as a string, otherwise it is separates the elements.
  • $subject:$subject - this is a variable that is used to store the input string.
  • $limit:$limit indicates a limit. If a limit is specified, then the small or substring must be returned up to the limit. If the limit is 0 or -1, it means there is no limit, and then used by the flag ($strflag).
  • $flags:$flags is used for signalize, and the type of its variable indicates two states True or False for controlling the program. Its combinations of various flags, such as the following:
    • PREG_SPLIT_NO_EMPTY:If the flag variable is set to PREG_SPLIT_NO_EMPTY, then only non-empty chunks will be returned by preg_split().
    • PREG_SPLIT_DELIM_CAPTURE:If the flag variable is set to PREG_SPLIT_DELIM_CAPTURE, the parenthesized expression in the delimiter pattern will also be captured and returned.
    • PREG_SPLIT_OFFSET_CAPTURE for the variable:-flag is set to PREG_SPLIT_OFFSET_CAPTURE, for every match it encounters, an auxiliary line offset will be returned, which changes the return value in the array, where the matching line offset will be 0 and the input line offset will be 1.
Return Value:This function returns an array after matching the split boundaries. When the limit of the original array or string is exceeded, it is returned with an array element, otherwise it is False.The following programs illustrate the preg_split() function in PHP:
Program 1:   // Input line $inputstrVal  = ’Geeksarticle’ ;  
// Implementation of the preg_split() function $result = preg_split ( ’//’ , $inputstrVal , -1, PREG_SPLIT_NO_EMPTY);  
// Show result print_r ( $result ); ?>
Exit:
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 program of the preg_split() function
// split the phrase into any number of commas
// spaces include / r, / t, / n and / f  $result = preg_split ( "/ [s,] + / " , " Geeks for Geeks " );  
// Show result print_r ( $result ); ?>
Exit:
Array ([ 0] = > Geeks [1] = > for [2] = > Geeks) 
Program 3:  
// PHP program to implement
// preg_split() function 
// Enter the original string $inputstrVal = " http://php.net /archive/2018.php " ; $patternstrVal = "/ [http : // |.] / " ;  
// Implement the preg_split() function $result = preg_split ( $patternstrVal , $inputstrVal , 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE);  
// Show result print_r ( $result ); ?>
Exit:
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)) 
Link: http://php.net/manual/en/ function.preg-split.php