3v4l.org

run code in 300+ PHP versions simultaneously
<?php $lines = [ 'Alice Sandy', 'Alice Nanami', 'James Watt', 'Alice Monica', 'Johann Gauss', 'Cooper Alice', ]; $needle = 'Alice'; foreach($lines as $index => &$line) { if ($needle === strstr($line, ' ', true)) { // check whole first word if (!isset($firstOccurrence)) { $firstOccurrence =& $line; } else { $firstOccurrence .= strstr($line, ' '); unset($lines[$index]); } } } var_export($lines);
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.16, 8.5.0 - 8.5.1
array ( 0 => 'Alice Sandy Nanami Monica', 2 => 'James Watt', 4 => 'Johann Gauss', 5 => 'Cooper Alice', )

preferences:
196.2 ms | 408 KiB | 5 Q