3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $splitby = array('these','are','the','words','to','split','by'); $text = 'This is the string which needs to be split by the above words.'; $split = explode(' ', $text); $result = array(); $temp = array(); foreach ($split as $s) { if (in_array($s, $splitby)) { if (sizeof($temp) > 0) { $result[] = implode(' ', $temp); $temp = array(); } } else { $temp[] = $s; } } if (sizeof($temp) > 0) { $result[] = implode(' ', $temp); } var_dump($result);
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
array(4) { [0]=> string(7) "This is" [1]=> string(18) "string which needs" [2]=> string(2) "be" [3]=> string(12) "above words." }