

<?php
$bunchOfHay='Data set 1: hay2= this is a bunch of hay  hay1= Gold_Needle hay=Gold
			 Data Set 2: hay2=Silver_Needle hay=Silver';

$needle1_Begin='hay1=';
$needle2_Begin='hay2=';
			
$needle1_End='hay=Gold';
$needle2_End='hay=Silver';

	
	
$starts = array($needle1_Begin,$needle2_Begin);
$ends = array($needle1_End,$needle2_End);

$haystack = $bunchOfHay;

function extract_unit($haystack, $starts, $ends){

    $reg = '/(?:' . implode('|', $starts) . ')\s*(.*?)\s*(?:' . implode('|', $ends) . ')/';

    preg_match_all($reg, $haystack, $return);

    return $return;

}

print_r (extract_unit($haystack, $starts, $ends)[1]);

?>

Result is:

Array ( [0] => this is a bunch of hay hay1= Gold_Needle [1] => Silver_Needle )
*/

/*
Desired Result is:

Array ( [0] => Gold_Needle [1] => Silver_Needle )
*/