<?php $str = "this is a test"; $one_words = array("a test", "take off", "go away", "depend on"); // To split the string per <space> $arr = explode(' ', $str); // To remove empty elements $arr = array_filter($arr); foreach ( $arr as $k => $v) { if ( isset($arr[$k+1])) { $combined_word = $arr[$k] . ' ' . $arr[$k+1]; if ( in_array($combined_word, $one_words) ){ $arr[$k] = $combined_word; unset($arr[$k+1]); } } } print_r($arr);
You have javascript disabled. You will not be able to edit any code.