Category Archives: PHP

PHP – Split with delimiter caputre

Split a string on a regular expression and also capture the delimiter.  This can be useful if you have regular expressions like /(cat|dog|fish)/.

$arr = preg_split($rx, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
for ($i = 0; $i < count($arr); $i+=2) {
  $chunk = $arr[0];
  $delim = $arr[1];
}