3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $string = "HD Alcoa Earnings Soar; Outlook Stays Upbeat BY By James R. Hagerty and Matthew Day PD 12 July 2011 LP Alcoa Inc.'s profit more than doubled in the second quarter. The giant aluminum producer managed to meet analysts' forecasts. However, profits wereless than expected TD Licence this article via our website: http://example.com"; function headerSplit($input) { static $delimsExpr = '/^(HD|BY|PD|LP|TD)/m'; static $whiteExpr = '/\s+/'; $parts = preg_split($delimsExpr, $input, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); $result = array(); $i = 0; $l = count($parts); while ($i < $l) { $result[$parts[$i++]] = preg_replace($whiteExpr, ' ', trim($parts[$i++])); } return $result; } var_dump(headerSplit($string));
Output for 4.3.0 - 7.1.0
array(5) { ["HD"]=> string(41) "Alcoa Earnings Soar; Outlook Stays Upbeat" ["BY"]=> string(35) "By James R. Hagerty and Matthew Day" ["PD"]=> string(12) "12 July 2011" ["LP"]=> string(165) "Alcoa Inc.'s profit more than doubled in the second quarter. The giant aluminum producer managed to meet analysts' forecasts. However, profits wereless than expected" ["TD"]=> string(56) "Licence this article via our website: http://example.com" }