PHP Regular expression preg split


the preg_split() function is used for regular expression splitting a string.

grammar:

Array preg_split(string pattern, string subject[, int limit[, int flags]])

Returns an array containing the substrings that were split along the boundary matching pattern.

Parameters Description
pattern regular expression
subject need to match the split object
limit Optional. If limit is specified, a maximum of limit substrings will be returned. If limit is -1, there is no limit and can be used to specify the optional flags
flags

The optional limit is set to -1 and can be any combination of the following flags(combined with the bitwise | operator):

  1. PREG_SPLIT_NO_EMPTY: preg_split() returns only non-empty components
  2. PREG_SPLIT_DELIM_CAPTURE: The bracket expression in the delimiter pattern is also captured and returned
  3. PREG_SPLIT_OFFSET_CAPTURE: Returns a string offset for each occurrences of the matching results. Note that this changes the value of the returned array so that each cell in it is also an array, where the first term is the matching string and the second is its offset in the subject.

Example 1:

$str = "php mysql, apache ajax";
$keywords = preg_split("/[\ s,] + /", $str);
print_r($keywords);

The output is:

Array
(
   [0] => php
   [1] => mysql
   [2] => apache
   [3] => ajax
)

Example 2:

$str = 'string';
$chars = preg_split('// ', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);

The output is:

(
   [0] => s
   [1] => t
   [2] => r
   [3] => i
   [4] => n
   [5] => g
)

Example 3:

$str = "php mysql, apache ajax";
$keywords = preg_split("/[\ s,] + /", $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
print_r($keywords);

The output is:

Array
(
   [0] => Array
      (
           [0] => php
           [1] => 0
        )
 
   [1] => Array
      (
           [0] => mysql
           [1] => 4
        )
 
   [2] => Array
      (
           [0] => apache
           [1] => 10
        )
 
   [3] => Array
      (
           [0] => ajax
           [1] => 17
        )
)

split()

the split() function is similar to preg_split(), and uses a regular expression to split the string into an array, returning an array, but preg_split() is recommended.

grammar:

Array split(string pattern, string string[, int limit])

If limit is set, the returned array contains up to limit units, and the last unit contains all the remaining parts of string. If an error occurs, FALSE is returned.

example:

$date = "2008-08-08 20: 00: 01";
print_r(split('[-:]', $date));

Output:

Array
(
   [0] => 2008
   [1] => 08
   [2] => 08
   [3] =>
   [4] => 00
   [5] =>
)

Tip

  1. If you do not need the regular expression function, you can choose to use faster(and simpler) alternative functions such as explode() or str_split().
  2. The split() function is case-sensitive, and if you are ignoring case differences when matching alphabetic characters, use the same spliti() function



Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook