9951 explained code solutions for 126 technologies


php-regexHow to split string by regex in PHP?


Splitting a string by regex in PHP can be done using the preg_split() function.

$string = 'This is a string';
$regex = '/\s+/';
$words = preg_split($regex, $string);

print_r($words);

The output of the above code will be:

Array
(
    [0] => This
    [1] => is
    [2] => a
    [3] => string
)

The preg_split() function takes two parameters:

  1. $regex - The regular expression to use for splitting the string.
  2. $string - The string to split.

The function will return an array of strings split by the regular expression.

Helpful links

Edit this code on GitHub