fork download
  1.  
  2.  
  3. <?php
  4. $haystack='Data set 1: hay2= this is a bunch of hay hay1= Gold_Needle hay=Gold
  5. Data Set 2: hay2=Silver_Needle hay=Silver';
  6.  
  7. $needle1_Begin='hay1=';
  8. $needle2_Begin='hay2=';
  9.  
  10. $needle1_End='hay=Gold';
  11. $needle2_End='hay=Silver';
  12.  
  13. $starts = array($needle1_Begin,$needle2_Begin);
  14. $ends = array($needle1_End,$needle2_End);
  15.  
  16. $re = array_reduce($starts, function($res, $e) use (&$ends) {
  17. $res .= '(' . $e . '\h*\K(?:.(?!' . $e . '))*?(?= ' . current($ends) . '))|';
  18. next($ends); return $res;} );
  19. $re = '/' . substr($re, 0, -1) . '/';
  20.  
  21. if (preg_match_all($re, $haystack, $m))
  22. print_r($m[0]);
  23. ?>
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout

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