fork(2) download
  1.  
  2.  
  3. <?php
  4. $bunchOfHay='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.  
  14.  
  15. $starts = array($needle1_Begin,$needle2_Begin);
  16. $ends = array($needle1_End,$needle2_End);
  17.  
  18. $haystack = $bunchOfHay;
  19.  
  20. function extract_unit($haystack, $starts, $ends){
  21.  
  22. $reg = '/(?:' . implode('|', $starts) . ')\s*(.*?)\s*(?:' . implode('|', $ends) . ')/';
  23.  
  24. preg_match_all($reg, $haystack, $return);
  25.  
  26. return $return;
  27.  
  28. }
  29.  
  30. print_r (extract_unit($haystack, $starts, $ends)[1]);
  31.  
  32. ?>
  33.  
  34. Result is:
  35.  
  36. Array ( [0] => this is a bunch of hay hay1= Gold_Needle [1] => Silver_Needle )
  37. */
  38.  
  39. /*
  40. Desired Result is:
  41.  
  42. Array ( [0] => Gold_Needle [1] => Silver_Needle )
  43. */
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout

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

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 )
*/