4

I need to find all the strings placed between START and END, escluding PADDING substring from matched string. The best way I've found is

$r="stuffSTARTthisPADDINGisENDstuffstuffSTARTwhatPADDINGIwantPADDINGtoPADDINGfindENDstuff" ;
preg_match_all('/START(.*?)END/',str_replace('PADDING','',$r),$m);
print(join($m[1]));
> thisiswhatIwanttofind

I want to do this with the smallest code size possible: there a shorter with only preg_match_all and no str_replace, that eventually returns directly the string without join arrays? I've tried with some lookaround expressions but I can't find the proper one.

Emilio
  • 3,901
  • 11
  • 44
  • 50

3 Answers3

1
$r="stuffSTARTthisPADDINGisENDstuffstuffSTARTwhatPADDINGIwantPADDINGtoPADDINGfindENDstuff";
echo preg_replace('/(END.*?START|PADDING|^[^S]*START|END.*$)/', '', $r);

This should return you thisiswhatIwanttofind using a single regular expression pattern

Explanation:-

END.*?START  # Replace occurrences of END to START
PADDING      # Replace PADDING
^[^S]*START  # Replace any character until the first START (inclusive)
END.*$       # Replace the last END and until end of the string
Suleman C
  • 783
  • 4
  • 17
0
$r="stuffSTARTthisPADDINGisENDstuffstuffSTARTwhatPADDINGIwantPADDINGtoPADDINGfindENDstuff" ;
preg_match_all('/(?:START)(.*?)(?:END)/',str_replace('PADDING','',$r),$m);
var_dump(implode(' ',$m[1]));

would work but I guess you want something faster.

Shikiryu
  • 10,180
  • 8
  • 49
  • 75
0

You can also use use preg_replace_callback like this:

$str = preg_replace_callback('#.*?START(.*?)END((?!.*?START.*?END).*$)?#', 
           function ($m) {
               print_r($m);
               return str_replace('PADDING', '', $m[1]);
           }, $r);

echo $str . "\n"; // prints thisiswhatIwanttofind
anubhava
  • 761,203
  • 64
  • 569
  • 643