php - Match pattern and exclude substrings with preg_match_all -
i need find strings placed between start , end, escluding padding substring matched string. 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 smallest code size possible: there shorter preg_match_all , no str_replace, returns directly string without join arrays? i've tried lookaround expressions can't find proper one.
$r="stuffstartthispaddingisendstuffstuffstartwhatpaddingiwantpaddingtopaddingfindendstuff"; echo preg_replace('/(end.*?start|padding|^[^s]*start|end.*$)/', '', $r);
this should return thisiswhatiwanttofind
using single regular expression pattern
explanation:-
end.*?start # replace occurrences of end start padding # replace padding ^[^s]*start # replace character until first start (inclusive) end.*$ # replace last end , until end of string
Comments
Post a Comment